Road Improvement - CodeForces 543 D 树形dp

解决在一棵树状结构中,如何从每个节点作为起点,调整边的状态使得到其他所有节点的路径上至多只有一条坏边的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
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");
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值