Codeforces 741D. Arpa???s letter-marked tree and Mehrdad???s Dokhtar-kosh paths

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 741D. Arpa???s letter-marked tree and Mehrdad???s Dokhtar-kosh paths相关的知识,希望对你有一定的参考价值。

???????????????   lca   stdin   and   max   ???????????????   modify   printf   ??????   

Description

???(1) ?????? ??? (n) ???????????????,???????????????????????? (x),?????????????????????????????????????????????????????????
????????????????????????????????????????????????????????????????????????????????????????????????????????????
??????

Solution

??????????????? (dsu,on,tree),????????????????????????,??????????????????????????????,????????????????????????

??????????????????????????????????????????????????????????????????,??????????????? (22),??????????????????
??????????????????????????????????????????????????? (dis[x])^(dis[y])^(a[lca]),(dis[x]) ?????? (x) ???????????????????????????
??????????????????????????????????????????????????? (vector) ??? (map),???????????????????????????????????????
?????????????????????????????????????????????

?????? (dsu,on,tree) ??????:
??????????????????????????????????????????
????????????????????????:??????????????????????????????????????????,?????????????????????
??????????????????,??????????????????????????????????????????,????????????????????????????????????????????????
???????????????????????????,????????????????????????????????????,????????????????????????,?????????????????????????????????????????????????????????
????????????????????????????????????,??????????????????????????????,???????????????,??????????????????
????????????????????????????????????????????????,???????????????????????????

????????????????????? (O(n*logn)) ???,????????????????????????????????????????????? (log) ???,???????????????????????? (O(n*log)) ???

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
int n,head[N],nxt[N],num=0,to[N],c[N],sz[N],son[N];
inline void link(int x,int y){nxt[++num]=head[x];to[num]=y;head[x]=num;}
int ans[N],dep[N],f[1<<22],o;
inline void dfs(int x){
    sz[x]=1;
    for(int i=head[x];i;i=nxt[i]){
        int u=to[i];
        dep[u]=dep[x]+1;c[u]^=c[x];dfs(u);sz[x]+=sz[u];
        if(sz[u]>sz[son[x]])son[x]=u;
    }
}
inline void Delet(int x){
    f[c[x]]=-N;
    for(int i=head[x];i;i=nxt[i])Delet(to[i]);
}
inline void Modify(int x){
    ans[o]=max(ans[o],f[c[x]]+dep[x]);
    for(int i=0;i<22;i++)ans[o]=max(ans[o],f[c[x]^(1<<i)]+dep[x]);
    for(int i=head[x];i;i=nxt[i])Modify(to[i]);
}
inline void ins(int x){
    f[c[x]]=max(f[c[x]],dep[x]);
    for(int i=head[x];i;i=nxt[i])ins(to[i]);
}
inline void dfs1(int x){
    for(int i=head[x];i;i=nxt[i])
        if(to[i]!=son[x])dfs1(to[i]),Delet(to[i]);
    if(son[x])dfs1(son[x]);o=x;
    for(int i=head[x];i;i=nxt[i])
        if(to[i]!=son[x])Modify(to[i]),ins(to[i]);
    f[c[x]]=max(f[c[x]],dep[x]);
    ans[o]=max(ans[o],f[c[x]]+dep[x]);
    for(int i=0;i<22;i++)ans[o]=max(ans[o],f[c[x]^(1<<i)]+dep[x]);
    ans[o]-=dep[x]<<1;
    for(int i=head[x];i;i=nxt[i])ans[x]=max(ans[x],ans[to[i]]);
}
int main(){
  freopen("pp.in","r",stdin);
  freopen("pp.out","w",stdout);
  scanf("%d",&n);
  char ch[2];int x;
  for(int i=(1<<22)-1;i>=0;i--)f[i]=-N;
  for(int i=2;i<=n;i++){
      scanf("%d%s",&x,ch);
      link(x,i);
      c[i]=1<<(ch[0]-'a');
  }
  dfs(1);dfs1(1);
  for(int i=1;i<=n;i++)printf("%d ",ans[i]);
  return 0;
}

以上是关于Codeforces 741D. Arpa???s letter-marked tree and Mehrdad???s Dokhtar-kosh paths的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces.741D.Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree 思路)

Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses

codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

Codeforces 741B-Arpa's weak amphitheater and Mehrdad's valuable Hoses

http://codeforces.com/contest/741/problem/B B. Arpa's weak amphitheater and Mehrdad's valuab

Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths CodeForces - 741Ddsu on tree+异或