Road Improvement - CodeForces 543 D 树形dp
2015-05-16 18:41
344 查看
Road Improvement
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The country has n cities and n - 1 bidirectional
roads, it is possible to get from every city to any other one if you move only along the roads. The cities are numbered with integers from 1 to n inclusive.
All the roads are initially bad, but the government wants to improve the state of some roads. We will assume that the citizens are happy about road improvement if the path from the capital located in city x to
any other city contains at most one bad road.
Your task is — for every possible x determine the number of ways of improving the quality of some roads in order to meet the citizens'
condition. As those values can be rather large, you need to print each value modulo 1 000 000 007 (109 + 7).
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 2·105)
— the number of cities in the country. Next line contains n - 1positive integers p2, p3, p4, ..., pn (1 ≤ pi ≤ i - 1)
— the description of the roads in the country. Number pi means
that the country has a road connecting city pi and
city i.
Output
Print n integers a1, a2, ..., an,
where ai is
the sought number of ways to improve the quality of the roads modulo 1 000 000 007(109 + 7),
if the capital of the country is at city number i.
Sample test(s)
input
output
input
output
题意:在一棵树上,一开始所有的边都是坏的,如果以一个点作为首都,它到任意的其他点的每条路径上最多有一条边是坏的,则符合要求,问当1-n分别为首都的时候,修边的方式有多少种。
思路:首先dp1[i]表示以i为顶点,它下面的都是合法的情况的种类。dp1[u]=(dp1[v1]+1)*(dp1[v2]+1)*...如果u到v1的边为好的的话,那么dp[v1]就是合法即可,否则v1下面的所有边都要的好的,这种情况只有一个,所以是dp1[v1]+1。
dp2[i]表示从上往下的顶点合法情况,这个需要自己理解一下,其中用到了求逆元的方法。然后对于每一个点,就是求它的周围的点的dp+1的乘积。
AC代码如下:
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The country has n cities and n - 1 bidirectional
roads, it is possible to get from every city to any other one if you move only along the roads. The cities are numbered with integers from 1 to n inclusive.
All the roads are initially bad, but the government wants to improve the state of some roads. We will assume that the citizens are happy about road improvement if the path from the capital located in city x to
any other city contains at most one bad road.
Your task is — for every possible x determine the number of ways of improving the quality of some roads in order to meet the citizens'
condition. As those values can be rather large, you need to print each value modulo 1 000 000 007 (109 + 7).
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 2·105)
— the number of cities in the country. Next line contains n - 1positive integers p2, p3, p4, ..., pn (1 ≤ pi ≤ i - 1)
— the description of the roads in the country. Number pi means
that the country has a road connecting city pi and
city i.
Output
Print n integers a1, a2, ..., an,
where ai is
the sought number of ways to improve the quality of the roads modulo 1 000 000 007(109 + 7),
if the capital of the country is at city number i.
Sample test(s)
input
3 1 1
output
4 3 3
input
5 1 2 3 4
output
5 8 9 8 5
题意:在一棵树上,一开始所有的边都是坏的,如果以一个点作为首都,它到任意的其他点的每条路径上最多有一条边是坏的,则符合要求,问当1-n分别为首都的时候,修边的方式有多少种。
思路:首先dp1[i]表示以i为顶点,它下面的都是合法的情况的种类。dp1[u]=(dp1[v1]+1)*(dp1[v2]+1)*...如果u到v1的边为好的的话,那么dp[v1]就是合法即可,否则v1下面的所有边都要的好的,这种情况只有一个,所以是dp1[v1]+1。
dp2[i]表示从上往下的顶点合法情况,这个需要自己理解一下,其中用到了求逆元的方法。然后对于每一个点,就是求它的周围的点的dp+1的乘积。
AC代码如下:
#include<cstdio> #include<cstring> using namespace std; typedef long long ll; struct node { int v,next; }edge[400010]; void exgcd(ll a,ll b,ll &d,ll &x,ll &y) { if(!b) d=a,x=1LL,y=0LL; else exgcd(b,a%b,d,y,x),y-=x*(a/b); } ll inv(ll a,ll m) { ll d,x,y; exgcd(a,m,d,x,y); return d==1LL ? (x+m)%m : -1LL; } ll MOD=1e9+7,dp1[200010],dp2[200010]; int n,m,Head[200010],fa[200010],tot; bool vis[200010]; void add(int u,int v) { edge[tot].v=v; edge[tot].next=Head[u]; Head[u]=tot++; } void dfs1(int u) { int i,j,k,v; dp1[u]=1; for(i=Head[u];i!=-1;i=edge[i].next) { v=edge[i].v; if(v==fa[u]) continue; fa[v]=u; dfs1(v); dp1[u]=dp1[u]*(dp1[v]+1)%MOD; } } ll solve(int u,int k) { int i,j,v; ll ret=1; for(i=Head[u];i!=-1;i=edge[i].next) { v=edge[i].v; if(v==fa[u] || v==k) continue; ret=ret*(dp1[v]+1)%MOD; } return ret; } void dfs2(int u) { int i,j,k,f,v; ll ret; f=fa[u]; if(dp1[u]+1==MOD) { dp2[u]=solve(f,u)*dp2[f]%MOD; } else { dp2[u]=dp1[f]*dp2[f]%MOD*inv((dp1[u]+1)%MOD,MOD)%MOD; } dp2[u]=(dp2[u]+1)%MOD; for(i=Head[u];i!=-1;i=edge[i].next) { v=edge[i].v; if(v==fa[u]) continue; dfs2(v); } } int main() { int i,j,k,u,v; ll ans; scanf("%d",&n); memset(Head,-1,sizeof(Head)); for(u=2;u<=n;u++) { scanf("%d",&v); add(u,v); add(v,u); } dfs1(1); dp2[1]=1; for(i=Head[1];i!=-1;i=edge[i].next) dfs2(edge[i].v); for(i=1;i<=n;i++) printf("%I64d ",dp1[i]*dp2[i]%MOD); printf("\n"); }
相关文章推荐
- Codeforces 543 D Road Improvement 树形dp
- codeforces 23E 树形DP
- Codeforces 61D--树形dp
- 校内赛 Codeforces 815C. Karen and Supermarket 树形DP 解题报告
- Codeforces 418d Big Problems for Organizers [树形dp][倍增lca]
- codeforces 462 d Appleman and Tree(树形dp)
- (状态压缩dp)Codeforces 543 C-Remembering Strings
- Codeforces 835 F Roads in the Kingdom(树形dp)
- codeforces 600E. Lomsat gelral(教育场 树形dp)
- CodeForces 101D Castle(树形dp)
- codeforces 274B Zero Tree(树形dp)
- codeforces 219D D. Choosing Capital for Treeland(树形dp)
- Codeforces 161 D Distance in Tree 树形DP
- CodeForces - 219D Choosing Capital for Treeland【树形dp*好题】
- Codeforces 743D 树形dp
- codeforces 522A A. Reposts(树形dp)
- CodeForces 219D 树形DP
- Codeforces 23E (大数乘法+树形dp)
- codeforces 61D Eternal Victory 树形DP
- codeforces 219D Choosing Capital for Treeland(树形DP)