Codeforces Round #382 (Div. 1) C. Ostap and Tree(树形DP)
2016-12-06 17:30
543 查看
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).
Examples
input
output
input
output
input
output
input
output
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[0][u][i] 表示 以u为根的子树中离根最近的黑点距离根的距离为i的方案数,dp[1][u][i]表示以u为根的子树中离根最远的未满足点距离根的距离为i的方案数。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9#define MOD 1000000007
using namespace std;
int n,k,u,v;
long long ans,fl[25],fw[25],dp[2][105][25];
vector<int> G[105];
void dfs(int u,int fa)
{
for(int v : G[u])
if(v != fa) dfs(v,u);
dp[0][u][0] = dp[1][u][0] = 1; //满足条件最浅和不满足条件最深
for(int v : G[u])
if(v != fa)
{
memset(fl,0,sizeof(fl));
memset(fw,0,sizeof(fw));
for(int i = 0;i <= k;i++)
{
for(int j = 0;j <= k;j++) //win win
fw[min(i,j+1)] += dp[0][u][i] * dp[0][v][j] % MOD;
for(int j = 0;j <= k;j++) //win lose
if(k - i >= j+1) fw[i] += dp[0][u][i] * dp[1][v][j] % MOD;
else fl[j+1] += dp[0][u][i] * dp[1][v][j] % MOD;
}
for(int i = 0;i <= k;i++)
{
for(int j = 0;j <= k;j++) //lose win
if(k - j - 1 >= i) fw[j+1] += dp[1][u][i] * dp[0][v][j] % MOD;
else fl[i] += dp[1][u][i] * dp[0][v][j] % MOD;
for(int j = 0;j <= k;j++) // lose lose
fl[max(i,j+1)] += dp[1][u][i] * dp[1][v][j] % MOD;
}
for(int i = 0;i <= k;i++)
{
dp[0][u][i] = fw[i] % MOD;
dp[1][u][i] = fl[i] % MOD;
}
}
}
int main()
{
cin.sync_with_stdio(false);
cin>>n>>k;
for(int i = 1;i < n;i++)
{
cin>>u>>v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1,-1);
for(int i = 0;i <= k;i++) ans = (ans + dp[0][1][i]) % MOD;
cout<<ans<<endl;
}
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).
Examples
input
2 0 1 2
output
1
input
2 11 2
output
3
input
4 11 2
2 33 4
output
9
input
7 2
1 2
2 31 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[0][u][i] 表示 以u为根的子树中离根最近的黑点距离根的距离为i的方案数,dp[1][u][i]表示以u为根的子树中离根最远的未满足点距离根的距离为i的方案数。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9#define MOD 1000000007
using namespace std;
int n,k,u,v;
long long ans,fl[25],fw[25],dp[2][105][25];
vector<int> G[105];
void dfs(int u,int fa)
{
for(int v : G[u])
if(v != fa) dfs(v,u);
dp[0][u][0] = dp[1][u][0] = 1; //满足条件最浅和不满足条件最深
for(int v : G[u])
if(v != fa)
{
memset(fl,0,sizeof(fl));
memset(fw,0,sizeof(fw));
for(int i = 0;i <= k;i++)
{
for(int j = 0;j <= k;j++) //win win
fw[min(i,j+1)] += dp[0][u][i] * dp[0][v][j] % MOD;
for(int j = 0;j <= k;j++) //win lose
if(k - i >= j+1) fw[i] += dp[0][u][i] * dp[1][v][j] % MOD;
else fl[j+1] += dp[0][u][i] * dp[1][v][j] % MOD;
}
for(int i = 0;i <= k;i++)
{
for(int j = 0;j <= k;j++) //lose win
if(k - j - 1 >= i) fw[j+1] += dp[1][u][i] * dp[0][v][j] % MOD;
else fl[i] += dp[1][u][i] * dp[0][v][j] % MOD;
for(int j = 0;j <= k;j++) // lose lose
fl[max(i,j+1)] += dp[1][u][i] * dp[1][v][j] % MOD;
}
for(int i = 0;i <= k;i++)
{
dp[0][u][i] = fw[i] % MOD;
dp[1][u][i] = fl[i] % MOD;
}
}
}
int main()
{
cin.sync_with_stdio(false);
cin>>n>>k;
for(int i = 1;i < n;i++)
{
cin>>u>>v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1,-1);
for(int i = 0;i <= k;i++) ans = (ans + dp[0][1][i]) % MOD;
cout<<ans<<endl;
}
相关文章推荐
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
- 【Codeforces Round 263 (Div 2)D】【树形DP】Appleman and Tree 树上割k个黑点为k块的方案数
- Codeforces Round #263 Appleman and Tree(树形DP)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree 树dp 统计
- Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形DP)
- Codeforces Round #419 (Div. 2)-树形dp&依赖背包&-E. Karen and Supermarket
- Codeforces Round #294 (Div. 2) E. A and B and Lecture Rooms(倍增LCA+树形DP)
- Codeforces Round #343 (Div. 2) E. Famil Door and Roads (树形dp,lca)★ ★ ★
- Codeforces Round #430 (Div. 2) - C. Ilya And The Tree (树形DFS)
- Codeforces Round #267 (Div. 2) C. George and Job DP
- Codeforces Round #189 (Div. 1) Kalila and Dimna in the Logging Industry 斜率DP
- Codeforces Round #290 (Div. 2) D. Fox And Jumping dp
- Codeforces Round #362 (Div. 2) D 树形dp
- 【Codeforces Round 331 (Div 2)D】【DP 记忆化搜索 期望DP区间性质 好题】Wilbur and Trees 砍树随机从右从左概率左倒右倒的期望底面覆盖长度
- Codeforces Round #199 (Div. 2) E. Xenia and Tree
- Codeforces Round #365 (Div. 2)Mishka and Divisors 题解。 DP
- Codeforces Round #235 (Div. 2) D Roman and Numbers(状态DP)
- 【Codeforces Round 333 (Div 2)E】【期望DP概率做法 树状数组转前缀和】Kleofáš and the n-thlon n场比赛m个人获得总名次的期望
- Codeforces Round #135 (Div. 2) - D. Choosing Capital for Treeland(dfs / 树形DP)
- Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings [dp 前缀和 ]