[Loj2546] [JSOI2018] dive into the action (tree DP)

Title Description
aliens and dual Cheese Li to attack the Earth, the alien mother ship has sailed to the Earth! This time, JYY have good contact gold fleet, intends to unite all JSOIer against alien attack.

Before the gold fleet in place, JYY intend to advance understanding of the alien plan of attack. Now, I carry a listening device agents had sneaked into the alien mothership, ready to monitor the implementation of extraterrestrial communication.

Alien mother ship can be regarded as a node n, n-1 with 1,2-free edges to the tree, the tree nodes, \ cdots, n1,2, ⋯, n number. JYY agents have been equipped with stealth module, unlimited activities in the alien mothership can mysteriously installed a listening device on the node.

If the monitoring device is mounted on a node uu, it is possible to listen to the traffic JYY all nodes immediately adjacent to uu. In other words, if the monitoring device installed uu node, the tree for each edge (u, v), vv node will be listening. Special attention is placed on node uu uu listening device does not monitor the communication itself, which is particularly JYY to prevent alien perceived tactical deployment.

JYY agents kk a total carrying listening devices, now JYY want to know how many different ways there are to place listening devices, enables communication of all nodes on the mothership are listening? To avoid wasting, each node can be installed up to a monitoring device and the monitoring device have been exhausted.

Input format
input of the first line contains two integers n, kn, k, kk represents the number of listening devices and the number nn mother ship node. Next, n-1n-1 lines, each two integers u, vu, v (1 \ le u, v \ le n) (1≤u, v≤n), one side of the tree represents.

Output format
output line, represents a number satisfying the condition of the program. Because the answer may be large, you only need to output the answer \ (\ text {mod 1,000,000,007} \) the remainder can be.

Obviously DP tree

But the push state transition equation wanted to be dead!

\ (First talk about DP [i] [j] [ 0/1] [0/1] i represents the point of the tree, put a total j-th monitoring equipment, whether or not the point i is covered, whether there listening point i when the device listens finished covering subtree i (excluding i) how many programs \) .

\ (Obviously when we update dfs, the update is from the son of his father, so the above refers to coverage means coverage of his father's son. We also need to set up another array DP, set the array is dp1 \)

The next step is disgusting four cases discussed.

First, if the point is updated DP [u] [j] [0] [0].

Because u are not covered, so be sure there is no monitoring equipment on the son, but also because of the need to completely cover, u can not be covered for son, so the son must be covered.

dp[u][i+j][0][0]=(dp[u][i+j][0][0]+(dp1[i][0][0]*1ll*dp[v][j][1][0])%mod)%mod;

If DP [u] [j] [0] [1].

Because u are not covered, so be sure not listening devices son, and because there are listening devices on u, so the son has not covered all right.

dp[u][i+j][0][1]=(dp[u][i+j][0][1]+(dp1[i][0][1]*1ll*(dp[v][j][1][0]+dp[v][j][0][0])%mod)%mod)%mod;

If DP [u] [j] [1] [0].

Since u is covered, the state before update of u may not have been covered or to be covered, so dp may be updated by updating a state before two (0,0) or (1,0).

1. by the (0,0) update coming

In this case, since the final state requirements are covered, so the son must have monitoring equipment, but also because u son can not be covered, so the son must be covered.

2. by the (1,0) update coming

Similarly, since u been covered before, so there are no listening devices are on the son, son and because u can not be covered, so the son must be covered.

dp[u][i+j][1][0]=(dp[u][i+j][1][0]+((dp1[i][0][0]*1ll*dp[v][j][1][1])%mod+0ll+(dp1[i][1][0]*1ll*(dp[v][j][1][0]+dp[v][j][1][1])%mod)%mod)%mod)%mod;

If DP [u] [j] [1] [1].

Like the case, as this can also be a dp and two states before the update (0,1) or (1,1)

1. by the (0,1) update coming

Because there are listening devices on u, so there can not be covered by the son, and because u are not covered by (state requirements are covered), so there are listening devices son.

2. by the (1,1) update coming

Similarly, u but this has been covered, so there is no listening devices can be on the son.

dp[u][i+j][1][1]=(dp[u][i+j][1][1]+(dp1[i][0][1]*1ll*(dp[v][j][1][1]+dp[v][j][0][1])%mod)%mod+0ll+(dp1[i][1][1]*1ll*((((dp[v][j][1][0]+dp[v][j][1][1])%mod+dp[v][j][0][0])%mod+dp[v][j][0][1])%mod)%mod)%mod)%mod;

In summary, the four cases discussed classification has been completed, the main program also written out.

Two initialization:

dp[u][0][0][0]=dp[u][1][0][1]=1;
#include<bits/stdc++.h>
#define mod 1000000007
#define N 100010
#define M 110
using namespace std;
int n,m,to[N<<1],nxt[N<<1],head[N],cnt,size[N],x,y,dp[N][M][2][2],dp1[M][2][2];//哪一个点,子树放了几个,u是否覆盖,u是否放
void adde(int x,int y)
{
    to[++cnt]=y;
    nxt[cnt]=head[x];
    head[x]=cnt;
}
void dfs(int u,int fa)
{
    size[u]=1;
    dp[u][0][0][0]=dp[u][1][0][1]=1;
    for(int k=head[u];k;k=nxt[k])
    {
        int v=to[k];
        if(v!=fa)
        {
            dfs(v,u);
            for(int i=0;i<=m;i++)
            {
                dp1[i][0][0]=dp[u][i][0][0];
                dp[u][i][0][0]=0;
                dp1[i][1][0]=dp[u][i][1][0];
                dp[u][i][1][0]=0;
                dp1[i][0][1]=dp[u][i][0][1];
                dp[u][i][0][1]=0;
                dp1[i][1][1]=dp[u][i][1][1];
                dp[u][i][1][1]=0;
            }
            for(int i=0;i<=min(size[u],m);i++)
            {
                for(int j=0;j<=min(size[v],m-i);j++)
                {
                    dp[u][i+j][0][0]=(dp[u][i+j][0][0]+(dp1[i][0][0]*1ll*dp[v][j][1][0])%mod)%mod;
                    dp[u][i+j][0][1]=(dp[u][i+j][0][1]+(dp1[i][0][1]*1ll*(dp[v][j][1][0]+dp[v][j][0][0])%mod)%mod)%mod;
                    dp[u][i+j][1][0]=(dp[u][i+j][1][0]+((dp1[i][0][0]*1ll*dp[v][j][1][1])%mod+0ll+(dp1[i][1][0]*1ll*(dp[v][j][1][0]+dp[v][j][1][1])%mod)%mod)%mod)%mod;
                    dp[u][i+j][1][1]=(dp[u][i+j][1][1]+(dp1[i][0][1]*1ll*(dp[v][j][1][1]+dp[v][j][0][1])%mod)%mod+0ll+(dp1[i][1][1]*1ll*((((dp[v][j][1][0]+dp[v][j][1][1])%mod+dp[v][j][0][0])%mod+dp[v][j][0][1])%mod)%mod)%mod)%mod;
                }
            }
            size[u]+=size[v];
        }
    }
}
int main()
{
//  freopen("1.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        adde(x,y);
        adde(y,x);
    }
    dfs(1,-1);
    printf("%d\n",(dp[1][m][1][0]+dp[1][m][1][1])%mod);
    return 0;
}

Attachment: DP seems to burst open space will long long, so write a good mod it! ! !

Guess you like

Origin www.cnblogs.com/2017gdgzoi44/p/11780497.html