本文版权归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的更多相关文章

  1. 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,求每个子树中最长的边,满 ...

  2. 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 思路: 树上启发式合并 从根节点出发到每个位置的每个字符的奇偶性记为每个位 ...

  3. 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的 ...

  4. 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 ...

  5. 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 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  6. 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 ...

  7. [Codeforces741D]Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths——dsu on tree

    题目链接: Codeforces741D 题目大意:给出一棵树,根为$1$,每条边有一个$a-v$的小写字母,求每个点子树中的一条最长的简单路径使得这条路径上的边上的字母重排后是一个回文串. 显然如果 ...

  8. CF741 D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    题目意思很清楚了吧,那么我们从重排回文串的性质入手. 很容易得出,只要所有字符出现的次数都为偶数,或者有且只有一个字符出现为奇数就满足要求了. 然后想到什么,Hash?大可不必,可以发现字符\(\in ...

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

    感觉dsu on tree一定程度上还是与点分类似的.考虑求出跨过每个点的最长满足要求的路径,再对子树内取max即可. 重排后可以变成回文串相当于出现奇数次的字母不超过1个.考虑dsu on tree ...

随机推荐

  1. shell_03

    函数: fanction print_welcome(){ echo welcome now time is `date` } print_welcome 函数调用 print _welcome 00 ...

  2. 思辨“从外至内的认识和表达”——By Me at 20140928

                                                              从下面几个维度,来思辨“从外至内的认识和表达” [思考维度1]提到研发前期的架构工作 ...

  3. vs2010帮助文件安装完全攻略

    1.VS2010帮助文件不支持重新配置,这个时候打开C:\Program Files\Microsoft Help Viewer\1.0目录,找到“HelpLibManager.exe.config” ...

  4. 004-hadoop家族概述

    hadoop家族 名称 简介   Hadoop 分布式基础架构 Hadoop的框架最核心的设计就是:HDFS和MapReduce.HDFS为海量的数据提供了存储,则MapReduce为海量的数据提供了 ...

  5. POJ1269:Intersecting Lines(判断两条直线的关系)

    题目:POJ1269 题意:给你两条直线的坐标,判断两条直线是否共线.平行.相交,若相交,求出交点. 思路:直线相交判断.如果相交求交点. 首先先判断是否共线,之后判断是否平行,如果都不是就直接求交点 ...

  6. Git的安装和设置

    1.客户端下载 首先可以在https://git-scm.com/downloads下载客户端,进行安装. 2.安装 安装比较简单,可以直接默认一步一步往下安装即可: 3.配置github的ssh秘钥 ...

  7. String,StringBuffer和StringBuilder的区别

    面试的时候经常问到的一个题:这里先说明下三者在JVM中的执行速度:StringBuilder > StringBuffer > String,原因且看下面慢慢阐述. 首先看一个概念,为啥一 ...

  8. 微信小程序组件slider

    表单组件slider:官方文档 Demo Code: var pageData = {} for (var i = 1; i < 5; i++) { (function (index) { pa ...

  9. 131. Palindrome Partitioning(回文子串划分 深度优先)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  10. Problem I. Increasing or Decreasing MIPT-2016 Pre-Finals Workshop, Taiwan NTU Contest, Sunday, March 27, 2016

    题面: Problem I. Increasing or DecreasingInput file: standard inputOutput file: standard outputTime li ...