data structures dfs and similar trees *2900

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)
#define endl '\n'
#define eps 0.000000001
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const int INF=0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int maxn=5e5+5;
int tot,head[maxn];
struct E{
    int to,next,w;
}edge[maxn<<1];
void add(int u,int v,int w){
    edge[tot].to=v;
    edge[tot].w=w;
    edge[tot].next=head[u];
    head[u]=tot++;
}
int dep[maxn],siz[maxn],son[maxn],dis[maxn],L[maxn],R[maxn],id[maxn],dfn;
void dfs1(int u,int f){
    dep[u]=dep[f]+1;
    siz[u]=1;
    L[u]=++dfn;
    id[dfn]=u;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(v==f) continue;
        dis[v]=dis[u]^edge[i].w;
        dfs1(v,u);
        siz[u]+=siz[v];
        if(siz[v]>siz[son[u]]) son[u]=v;
    }
    R[u]=dfn;
}
int ans[maxn],flag,f[maxn*10];
void calc(int u,int fa){
    if(f[dis[u]]) ans[u]=max(ans[u],f[dis[u]]-dep[u]);
    rep(i,0,21){
        if(f[dis[u]^(1<<i)]){
            ans[u]=max(ans[u],f[dis[u]^(1<<i)]-dep[u]);
        }
    }
    f[dis[u]]=max(f[dis[u]],dep[u]);
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(v==fa||v==son[u]) continue;
        rep(j,L[v],R[v]){
            int x=id[j];
            if(f[dis[x]]) ans[u]=max(ans[u],f[dis[x]]+dep[x]-2*dep[u]);
            rep(k,0,21){
                if(f[dis[x]^(1<<k)]){
                    ans[u]=max(ans[u],f[dis[x]^(1<<k)]+dep[x]-2*dep[u]);
                }
            }
        }
        rep(j,L[v],R[v]) f[dis[id[j]]]=max(f[dis[id[j]]],dep[id[j]]);
    }
}
void dfs(int u,int fa,int keep){
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(v==son[u]||v==fa) continue;
        dfs(v,u,0);
        ans[u]=max(ans[u],ans[v]);
    }
    if(son[u]){
        dfs(son[u],u,1);
        ans[u]=max(ans[u],ans[son[u]]);
        flag=son[u];
    }
    calc(u,fa);
    flag=0;
    if(!keep){
        rep(i,L[u],R[u]) f[dis[id[i]]]=0;
    }
}
int main(){
    int n;cin>>n;mem(head,-1);
    rep(i,2,n){
        int x;char s;
        cin>>x>>s;
        add(i,x,1LL<<(s-'a'));
        add(x,i,1LL<<(s-'a'));
    }
    dfs1(1,0);
    dfs(1,0,0);
    rep(i,1,n) cout<<ans[i]<<" ";
}


Comments

Submit
0 Comments
More Questions

349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera