[dsu on tree] Codeforces #741D. Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths
考虑一个字符集,若能排成回文串,则出现个数是奇数的字符种数是0或1。字符有22种,就想到异或。
合法的链 (x,y)(x,y) 满足 s(x) xor s(y)=0或2is(x)\text{ xor }s(y)=0或2^{i}。
直接 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;
}
[dsu on tree] Codeforces #741D. Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths相关推荐
- ☆【CodeForces - 764C】Timofey and a tree (思维题,树的性质)
题干: Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After tha ...
- linux tree命令源码,[转帖]linux tree命令--显示目录的树形结构
linux tree命令--显示目录的树形结构 版权声明:iamqilei@qq.com https://blog.csdn.net/u011729865/article/details/533684 ...
- Codeforces Round #316 (Div. 2) D. Tree Requests dfs序
题目链接: 题目 D. Tree Requests time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 Rom ...
- Codeforces 742B Arpa’s obvious problem and Mehrdad’s terrible solution
http://codeforces.com/contest/742/problem/B B. Arpa's obvious problem and Mehrdad's terrible solutio ...
- CodeForces - 850C Arpa and a game with Mojtaba(博弈+sg函数)
题目链接:点击查看 题目大意:给出n个数,两个人轮流按照规则操作,不能操作的人即为失败,规则很简单,每次找一个质数p的k次幂,记做x=,将数组中所有包含x倍数的数都除以x,必须保证至少有一个数可以除以 ...
- 15行代码AC——Link/Cut Tree CodeForces - 614A(爆long long处理+快速幂讲解)
励志用少的代码做高效表达 Problem describe Programmer Rostislav got seriously interested in the Link/Cut Tree dat ...
- Appleman and Tree CodeForces - 461B(树形dp)
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...
- Paint the Tree CodeForces - 1244D(看似是树,其实是条链)
目录 题目 官方题解: 百度翻译 题解 ac代码 题目 给多组两顶点连接,得到的图任意三个顶点都是不同的颜色,,给出各顶点染三种颜色的花费,问各店如何染,满足条件情况下,使得花费最少: You are ...
- 【CodeForces - 1153D】Serval and Rooted Tree(树形dp)
题干: Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on ...
- 【CodeForces - 260D】Black and White Tree (思维构造,猜结论,细节,构造一棵树)
题干: The board has got a painted tree graph, consisting of n nodes. Let us remind you that a non-dire ...
最新文章
- Spring MVC-处理程序映射(Handler Mapping)-简单的Url处理程序映射(Simple Url Handler Mapping)示例(转载实践)...
- Cash Shuffle的BCH在稳步测试中前进
- “365算法每日学计划”:03打卡-贪心算法
- 一步一步学Ruby(十一):控制语句
- (二)oracle的SGA
- 2016年度十大新兴技术出炉,你最看好哪个?
- 学java教程之面向对象(四)
- Flutter 学习汇总
- linuxYUM源配置问题
- (7)Redis-Cluster集群理论及实践【上】
- html阿里矢量图标库,矢量图标素材库_阿里巴巴矢量图标库的使用
- 教育行业剧变:校讯通将死 家校沟通永生
- 超越QQ Mail文件中转站---大文件上传设计思路和实践 原创 王泽宾
- 计算机专业数字逻辑,大学计算机专业数字逻辑课程试卷及答案(4份)
- 再看2019大前端技术趋势,Web OS概念正落地
- java pdf 添加图片
- Ubuntu修改时区和更新时间
- TensorFlow深度学习:3.API示范
- 降低指定进程的CPU占用率(适合游戏多开)
- 数据结构—串的详细解释(含KMP算法)
热门文章
- r library car_R 语言教程:方差分析与多重比较
- python中文乱码如何处理、如何处理跨域_解决Python发送Http请求时,中文乱码的问题...
- WinRM(Windows远程管理)介绍,Python远程操作Windows
- DHT11温湿度传感器(zigbee)
- vue中如何引入公共样式的的styl文件
- 恒凯USB数据采集卡在国产麒麟操作系统下的应用(二)Python环境设置及数据采集实验
- wps怎么做文档分享
- conda Collecting package metadata (repodata.json)卡住或 failed问题
- 软件图标显示不正常的问题
- 自定义OrderedMap