[dsu on tree] Codeforces #741D. Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths
来源:互联网 发布:水经注软件 谷歌 编辑:程序博客网 时间:2024/05/12 12:44
考虑一个字符集,若能排成回文串,则出现个数是奇数的字符种数是0或1。字符有22种,就想到异或。
合法的链
直接 dsu on tree 瞎搞。
#include<cstdio>#include<vector>#include<algorithm>using namespace std;const int maxn=500005,maxe=500005;int n,ans[maxn],dfn[maxn],df[maxn],out[maxn],Tim,s[maxn],sz[maxn],dep[maxn],hvy[maxn];int fir[maxn],nxt[maxe],son[maxe],w[maxe],tot;int d_max[8388615],res;void add(int x,int y,int z){ son[++tot]=y; w[tot]=z; nxt[tot]=fir[x]; fir[x]=tot;}void dfs_info(int x,int pre){ dfn[x]=++Tim; df[Tim]=x; sz[x]=1; for(int j=fir[x];j;j=nxt[j]) if(son[j]!=pre){ dep[son[j]]=dep[x]+1; s[son[j]]=s[x]^(1<<w[j]); dfs_info(son[j],x); sz[x]+=sz[son[j]]; if(sz[hvy[x]]<sz[son[j]]) hvy[x]=son[j]; } out[x]=Tim;}void AllClear(int x){ res=0; for(int i=dfn[x];i<=out[x];i++) d_max[s[df[i]]]=0;}void dfs(int x,int pre){ for(int j=fir[x];j;j=nxt[j]) if(son[j]!=pre) if(son[j]!=hvy[x]) dfs(son[j],x), AllClear(son[j]); if(hvy[x]) dfs(hvy[x],x); res=ans[hvy[x]]; if(d_max[s[x]]) res=max(res,d_max[s[x]]-dep[x]); for(int i=0;i<=21;i++) if(d_max[s[x]^(1<<i)]) res=max(res,d_max[s[x]^(1<<i)]-dep[x]); d_max[s[x]]=max(d_max[s[x]],dep[x]); for(int j=fir[x];j;j=nxt[j]) if(son[j]!=pre) if(son[j]!=hvy[x]){ res=max(res,ans[son[j]]); for(int i=dfn[son[j]];i<=out[son[j]];i++){ int y=df[i]; if(d_max[s[y]]) res=max(res,d_max[s[y]]+dep[y]-2*dep[x]); for(int i=0;i<=21;i++) if(d_max[s[y]^(1<<i)]) res=max(res,d_max[s[y]^(1<<i)]+dep[y]-2*dep[x]); } for(int i=dfn[son[j]];i<=out[son[j]];i++){ int y=df[i]; d_max[s[y]]=max(d_max[s[y]],dep[y]); } } ans[x]=res;}int main(){ freopen("cf741D.in","r",stdin); freopen("cf741D.out","w",stdout); scanf("%d",&n); for(int i=2;i<=n;i++){ int x; char ch; scanf("%d %c",&x,&ch); add(x,i,ch-'a'); } dep[1]=1; dfs_info(1,1); dfs(1,1); for(int i=1;i<=n;i++) printf("%d ",ans[i]); return 0;}
阅读全文
0 0
- codeforces 741 D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths (dsu on the tree)
- [dsu on tree] Codeforces #741D. Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths
- 【dsu on tree】Codeforces741D[Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths]题解
- [Codeforces741D]Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on the tree)
- Codeforces 741D D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 树上启发式合并
- [树上启发式合并 && 哈希] Codeforces 741D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
- codeforces 742d D - Arpa's weak amphitheater and Mehrdad's valuable Hoses
- Codeforces Round #383 (Div. 2)-D. Arpa's weak amphitheater and Mehrdad's valuable Hoses
- CodeForces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses
- 【42.86%】【codeforces 742D】Arpa's weak amphitheater and Mehrdad's valuable Hoses
- Arpa's weak amphitheater and Mehrdad's valuable Hoses CodeForces - 742D
- Arpa’s obvious problem and Mehrdad’s terrible solution CodeForces
- Arpa’s obvious problem and Mehrdad’s terrible solution (CodeForces
- Arpa's weak amphitheater and Mehrdad's valuable Hoses CodeForces
- Arpa's weak amphitheater and Mehrdad's valuable Hoses CodeForces
- [dsu] codeforces 375D. Tree and Queries
- Codeforces 741A Arpa's loud Owf and Mehrdad's evil plan(思维)
- CodeForces 741C Arpa’s overnight party and Mehrdad’s silent entering
- 李宏毅机器学习2016 第三讲 梯度下降
- [NLP-关键词提取]关于nlp的一些实践
- 《编程珠玑(第2版)》【PDF】下载
- solr服务
- 勿忘初心,保持饥渴的心态
- [dsu on tree] Codeforces #741D. Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths
- System.Data.SqlClient.SqlError: 备份集中的数据库备份与现有的 'XXX' 数据库不同
- CULA安装配置和调试
- JAVA中request.getParameterMap()
- ElasticSearch 入门实例
- LAMP架构介绍
- 在线测试--图
- 14.大小写字母互换
- ScrollView 相关判断