zoukankan      html  css  js  c++  java
  • Codeforces Round #382 (Div. 2)E. Ostap and Tree

    E. Ostap and Tree
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 kDistance 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).

    Examples
    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}.

    一道神奇的树形dp。

     1 #include <bits/stdc++.h>
     2 const int mod = 1000000007;
     3 using namespace std;
     4 int n,k,cnt = 0,head[103];
     5 struct node
     6 {
     7     int nxt,to;
     8 }edge[205];
     9 long long int dp[105][255],f[255];
    10 
    11 
    12 void add(int a,int b)
    13 {
    14     edge[++cnt].to = b,edge[cnt].nxt = head[a];head[a] = cnt;
    15 }
    16 
    17 void Init()
    18 {
    19     scanf("%d%d",&n,&k);
    20     int a,b; memset(head,-1,sizeof(head));
    21     for(int i = 1; i < n; ++i)
    22     {
    23         scanf("%d%d",&a,&b);
    24         add(a,b);add(b,a);
    25     }
    26 }
    27 
    28 void dfs(int u,int pre)
    29 {
    30     dp[u][0] = dp[u][k+1] = 1;
    31     for(int i = head[u];~i; i = edge[i].nxt)
    32     {
    33         int v = edge[i].to;
    34         if(v == pre) continue;
    35         dfs(v,u);
    36         memset(f,0,sizeof(f));
    37         for(int x = 0; x <= 2 * k + 1; ++x)
    38         {
    39             for(int y = 0; y <= 2 * k; ++y)
    40             {
    41                 if(x + y <= 2*k) f[min(x,y+1)] = ( f[min(x,y+1)] + ( dp[u][x] * dp[v][y] % mod ) ) % mod ;
    42                 else f[max(x,y+1)] = ( f[max(x,y+1)] + ( dp[u][x] * dp[v][y] % mod ) ) % mod ;
    43             }
    44 
    45         }
    46         memcpy(dp[u], f, sizeof f);
    47     }
    48 
    49 }
    50 
    51 void Solve()
    52 {
    53     dfs(1,-1);
    54     long long int ans = 0ll;
    55     for(int i = 0; i <= k; ++i) ans = (ans + dp[1][i])%mod;
    56     cout<<ans<<endl;
    57 }
    58 
    59 int main()
    60 {
    61 //    freopen("E.in","r",stdin);
    62 //    freopen("E.out","w",stdout);
    63     Init();
    64     Solve();
    65     fclose(stdin);
    66     fclose(stdout);
    67     return 0;
    68 }
  • 相关阅读:
    TP5学习笔记- 使用命令行创建控制器
    centos 7 下安装mysql5.7
    webserver的安装
    linux常用命令 服务器硬件资源信息
    SSH 安装/ config 配置以及免密码登录
    thinkphp ,laravel,yii2运行环境搭建.
    分享几个博客园代码样式的CSS配置(复制黏贴即可)
    vue中通过.sync修饰符实现子组件修改父组件数据
    vue中$attrs和$listeners以及inheritAttrs的用法
    Vue项目中实现用户登录及token验证
  • 原文地址:https://www.cnblogs.com/Ateisti/p/6139225.html
Copyright © 2011-2022 走看看