CodeForces - 735E

先婊一下这个傻逼题
说了k肯定小于20
我他妈开了25为什么一直wa??????
开了70过了?????????????????
喵喵喵???????????????????????

Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph.

Ostap’s tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than k. Distance between two vertices of the tree is the minimum possible number of edges of the path between them.

As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one.

Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ min(20, n - 1)) — the number of vertices in Ostap’s tree and the maximum allowed distance to the nearest black vertex. Don’t miss the unusual constraint for k.

Each of the next n - 1 lines contain two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of vertices, connected by the i-th edge. It’s guaranteed that given graph is a tree.

Output
Print one integer — the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7).

Example
Input
2 0
1 2
Output
1
Input
2 1
1 2
Output
3
Input
4 1
1 2
2 3
3 4
Output
9
Input
7 2
1 2
2 3
1 4
4 5
1 6
6 7
Output
91
Note
In the first sample, Ostap has to paint both vertices black.

In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both.

In the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}.

这个题让求任意一个点方圆k长内一定要有一个黑的
dp[a][b]的 意思是说子树内距离点a最短有b距离的方案数…
在循环里用于枚举所有可能的方案数…
合法的放在最短的里面
可能不合法的放在超过k的里面
他可能合法,比如他旁边那个点在k以内,那么他可以在k以外着同样也是合法方案

有点绕…不过总算明白了

我对状态转移的理解还是太浅了

#include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
vector<int>tu[120];
long long  dp[120][70], zs[120],biaoji[120];
long long  n, k;
long long mod = 1e9 + 7;
void dfs(int gen)
{
    if (biaoji[gen])return;
    biaoji[gen] = 1;
    dp[gen][0] = dp[gen][k + 1] = 1;
    for (int a = 0; a < tu[gen].size(); a++)
    {
        if (biaoji[tu[gen][a]])continue;
        dfs(tu[gen][a]);
        memset(zs, 0, sizeof(zs));
        for (int b = 0; b <= 2*k; b++)
        {
            for (int c = 0; c <= 2 * k; c++)
            {
                if (b + c <= 2 * k)
                {
                    zs[min(b, c + 1)] += dp[gen][b] * dp[tu[gen][a]][c] % mod;
                    zs[min(b, c + 1)] %= mod;
                }
                else
                {
                    zs[max(b, c + 1)] += dp[gen][b] * dp[tu[gen][a]][c] % mod;
                    zs[max(b, c + 1)] %= mod;
                }
            }
        }
        for (int b = 0; b <= 2 * k; b++)dp[gen][b] = zs[b];
    }
}
int main()
{
#define int long long 
    cin >> n >> k;
    int q, w;
    for (int a = 1; a < n; a++)
    {
        cin >> q >> w;
        tu[q].push_back(w);
        tu[w].push_back(q);
    }
    dfs(1);
    int gs = 0;
    for (int a = 0; a <= k; a++)gs = (gs + dp[1][a]) % mod;
    cout << (gs+mod)%mod << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值