codeforces741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
题目链接:CF741D
正解:$dsu$ $on$ $tree$
解题报告:
对于这种回文串的问题,很显然出现次数为奇数的个数$<=1$,那么这个状态就可以状压了。
用$S$表示从这个点到根的字母奇偶性情况,因为$<=1$,我们特判掉$0$之后就只需要考虑有一位的情况,枚举这一位,可以唯一地得到一个状态,来满足条件,那么就可以做了。
考虑处理一个节点时,首先可以顺便处理掉从根到子树内某个点的情况,然后我们只需要考虑子树内两个点组合而成的情况。
用一个数组记一下每种状态的最大深度就$over$了…
有一个小$trick$:必须要用子树中的最优值往上更新…
//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
#include <bitset>
using namespace std;
typedef long long LL;
typedef long double LB;
typedef complex<double> C;
const double pi = acos(-1);
const int MAXN = 500011;
const int MAXM = 1000011;
int n,father[MAXN],ecnt,first[MAXN],to[MAXM],next[MAXM],size[MAXN],son[MAXN],deep[MAXN],ans[MAXN],Max,s[MAXN],f[10000011]/*!!!数组开小了!!!*/,inf;
char ch[MAXN];
inline void link(int x,int y){ next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; }
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void dfs(int x,int fa){
size[x]=1; if(fa) s[x]=s[fa]^(1<<(ch[x]-'a'));
for(int i=first[x];i;i=next[i]) {
int v=to[i]; deep[v]=deep[x]+1;
dfs(v,x); size[x]+=size[v];
if(size[v]>size[son[x]]) son[x]=v;
}
} inline void calc(int rt,int x){
int state=s[x];
Max=max(Max,f[state]+deep[x]-2*deep[rt]);
if ((s[x]^s[rt])==0) Max=max(Max,deep[x]-deep[rt]);
for (int i=0;i<22;++i) {
state=(1<<i)^s[x];
Max=max(Max,f[state]+deep[x]-2*deep[rt]);
if ((s[x]^s[rt])==(1<<i)) Max=max(Max,deep[x]-deep[rt]);
}
for (int i=first[x];i;i=next[i])
calc(rt,to[i]);
} inline void change(int x,int type){
if(type) f[s[x]]=max(f[s[x]],deep[x]);
else f[s[x]]=inf; for(int i=first[x];i;i=next[i])
change(to[i],type);
} inline void solve(int x,bool top){
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==son[x]) continue;
solve(v,1);
} if(son[x])
solve(son[x],0); Max=0; int S=s[x],nows;
Max=max(Max,f[S]-deep[x]);
for(int i=0;i<22;i++) {
nows=(1<<i)^s[x];
Max=max(Max,f[nows]-deep[x]);
} for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==son[x]) continue;
calc(x,v);
change(v,1);
} ans[x]=Max; if(top) {
for(int i=first[x];i;i=next[i])
change(to[i],0);
f[s[x]]=inf;
}
else f[s[x]]=max(f[s[x]],deep[x]);
} inline void upd(int x){
for(int i=first[x];i;i=next[i]) {
upd(to[i]);
ans[x]=max(ans[x],ans[to[i]]);
}
} inline void work(){
n=getint(); for(int i=2;i<=n;i++) father[i]=getint(),link(father[i],i),scanf("%c",&ch[i]);
deep[1]=0;
dfs(1,0); memset(f,-0x7f,sizeof(f)); inf=f[0];
solve(1,1);
upd(1);
for(int i=1;i<=n;i++)
printf("%d ",ans[i]);
} int main()
{
work();
return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
codeforces741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths的更多相关文章
- CF 741D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths [dsu on tree 类似点分治]
D. Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths CF741D 题意: 一棵有根树,边上有字母a~v,求每个子树中最长的边,满 ...
- Codeforces 741 D - Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
D - Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 思路: 树上启发式合并 从根节点出发到每个位置的每个字符的奇偶性记为每个位 ...
- CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 好像这个题只能Dsu On Tree? 有根树点分治 统计子树过x的 ...
- codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
题目链接:Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 第一次写\(dsu\ on\ tree\),来记录一下 \(dsu\ o ...
- 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 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...
- CF 741 D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths http://codeforces.com/problemset/probl ...
- [Codeforces741D]Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths——dsu on tree
题目链接: Codeforces741D 题目大意:给出一棵树,根为$1$,每条边有一个$a-v$的小写字母,求每个点子树中的一条最长的简单路径使得这条路径上的边上的字母重排后是一个回文串. 显然如果 ...
- CF741 D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
题目意思很清楚了吧,那么我们从重排回文串的性质入手. 很容易得出,只要所有字符出现的次数都为偶数,或者有且只有一个字符出现为奇数就满足要求了. 然后想到什么,Hash?大可不必,可以发现字符\(\in ...
- Codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)
感觉dsu on tree一定程度上还是与点分类似的.考虑求出跨过每个点的最长满足要求的路径,再对子树内取max即可. 重排后可以变成回文串相当于出现奇数次的字母不超过1个.考虑dsu on tree ...
随机推荐
- Django接收自定义http header(转)
add by zhj: Django将所有http header(包括你自定义的http header)都放在了HttpRequest.META这个Python标准字典中,当然HttpRequest. ...
- 从HD OJ 1005想到的
杭电OJ [1005](http://acm.hdu.edu.cn/showproblem.php?pid=1005): #####Problem Description > A number ...
- VS中的配置管理器
一. 活动解决方案配置 有Debug和Release两个基本选项. Debug:称为 调试版本,它包含调试信息,且不做任何优化,便于程序员调试: Release:称为 发布版本,它往往是进行 ...
- vs开发nodejs api文档生成神器-apidoc
直接生成文档的神器 apidoc 1 win+R 输入 cmd 回车 然后进入 nodejs 项目目录 例如 D:\NodeTest\newApp1 2 用npm安装 apidoc 直接输入 npm ...
- Python学习笔记(一)数据类型
一.整型和浮点型 Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样 age=10 num=-35 score=98.8 二.布尔类型 布尔值和布尔代数的表示 ...
- 在firefox安装Selenium IDE
1.打开地址:https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/: 2.点击Add to Firefox: 3.打开Firefox ...
- oracle dataguard参数
在整个dg配置中,最复杂的也许就是参数的配置了,并且有许多参数都可以延伸出去讲很多,所以今天我们来看看dg的参数配置,顺便加上一点dataguard进程相关的信息,帮助理解. 在配置dg的过程中,我们 ...
- 前端须知的http header
文件信息: Content-Type: application/x-javascript Content-Length: 2000 Content-Type:指定请求和响应的内容类型,如果未指定即为t ...
- 一、html
一.html相关概念 html是 htyper text markup language 即超文本标记语言,超文本就是指页面内可以包含图片.链接,甚至音乐.程序等非文字元素,而标记语言:即标记(标签) ...
- MySQL,sqlalchemy
Mariadb 数据库是一堆表的集合 主键 外键 索引 安装: Centos7 [root@host]# mysqladmin -u root password "new_password& ...