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 }
  • 相关阅读:
    【转载】 HTTP 中 GET 与 POST 的区别
    JS 浏览器cookie的设置,读取,删除
    JS Event事件流(冒泡机制、捕获机制、事件绑定)
    DOM节点树和元素树--深度遍历
    Html_Task4(知识点:水平居中+垂直居中/position/float/border-radius)
    百度前端技术学院—斌斌学院题库
    百度前端技术学院—-小薇学院(HTML+CSS课程任务)
    js正则表达式
    js设计模式(一)发布订阅模式
    vue学习笔记(一)——利用vue-cli搭建一个前端项目
  • 原文地址:https://www.cnblogs.com/Ateisti/p/6139225.html
Copyright © 2011-2022 走看看