题目链接:Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

  第一次写\(dsu\ on\ tree\),来记录一下

  \(dsu\ on\ tree\)主要维护子树信息,往往可以省掉一个数据结构的启发式合并。大体思路如下:

  轻重链路径剖分之后,对每个点先递归处理他的所有轻儿子,每次处理完轻儿子之后把这棵子树的信息清空。最后再来处理重孩子,重儿子的信息就可以不用清空了。由于我们是用一个全局数组来记录信息的,重儿子子树的信息就仍然保留在全局数组中。接着我们另外写一个函数\(dfs\)所有的轻儿子子树,并统计答案。每统计完一棵子树的答案就可以把这棵子树的信息计入全局数组中,用于下一次更新。由于每个点到根的轻边条数是\(\log n\)级别的,所以每个点最多被扫\(\log n\)遍。

  回到这道题上来。由于要求路径上的所有字符重新排列之后可以形成一个回文串,也就是说出现次数为奇数的字符不会超过\(1\)个。那么我们就可以给每个字符一个\(2^x\)形式的权值,这样的话合法路径的异或和要么为\(0\),要么为\(2^x\)的形式。

  设点\(x\)到根的异或和为\(D_x\),由于这道题是边权,\(x\)和\(y\)路径上的异或和就可以用\(D_x\ xor\ D_y\)来表示。这样的话就可以用一个数组\(c\)来统计答案,其中\(c_i\)表示满足\(D_x=i\)的\(x\)的最大深度。剩下的就是套\(dsu\ on\ tree\)的板子了。

  下面贴代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
#define maxn 500010
#define INF (1<<30) using namespace std;
typedef long long llg; int n,ci[1<<22],son[maxn],siz[maxn],dep[maxn];
int hd[maxn],nt[maxn],D[maxn],ans[maxn]; int getint(){
int w=0;bool q=0;
char c=getchar();
while((c>'9'||c<'0')&&c!='-') c=getchar();
if(c=='-') c=getchar(),q=1;
while(c>='0'&&c<='9') w=w*10+c-'0',c=getchar();
return q?-w:w;
} void dfs(int u){
siz[u]=1;
for(int i=hd[u];i;i=nt[i]){
D[i]^=D[u]; dep[i]=dep[u]+1;
dfs(i); siz[u]+=siz[i];
if(siz[i]>siz[son[u]]) son[u]=i;
}
} void undo(int u){
ci[D[u]]=-INF;
for(int i=hd[u];i;i=nt[i]) undo(i);
} int o;
void up(int &x,int y){if(y>x) x=y;}
void up(int u){
up(ans[o],dep[u]+ci[D[u]]);
for(int i=0;i<=21;i++) up(ans[o],dep[u]+ci[1<<i^D[u]]);
for(int i=hd[u];i;i=nt[i]) up(i);
} void ins(int u){
up(ci[D[u]],dep[u]);
for(int i=hd[u];i;i=nt[i]) ins(i);
} void work(int u){
for(int i=hd[u];i;i=nt[i])
if(i!=son[u]) work(i),undo(i);
if(son[u]) work(son[u]); o=u;
for(int i=hd[u];i;i=nt[i])
if(i!=son[u]) up(i),ins(i);
up(ci[D[u]],dep[u]);
up(ans[u],dep[u]+ci[D[u]]);
for(int i=0;i<=21;i++) up(ans[u],dep[u]+ci[1<<i^D[u]]);
ans[u]-=dep[u]<<1;
for(int i=hd[u];i;i=nt[i]) up(ans[u],ans[i]);
} int main(){
File("a");
n=getint();
for(int i=0;i<(1<<22);i++) ci[i]=-INF;
for(int i=2,x;i<=n;i++){
x=getint();
nt[i]=hd[x];hd[x]=i;
char c=getchar();
while(c>'v' || c<'a') c=getchar();
D[i]=1<<(c-'a');
}
dfs(1); work(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的更多相关文章

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

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

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

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

    题目链接 \(Description\) 给定一棵树,每条边上有一个字符(a~v).对每个节点,求它的子树中一条最长的路径,满足 路径上所有边上的字符可以重新排列成一个回文串.输出其最长长度. \(n ...

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

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

  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. codeforces 1065F Up and Down the Tree

    题目链接:codeforces 1065F Up and Down the Tree 题意:给出一棵树的节点数\(n\)以及一次移动的最大距离\(k\),现在有一个标记在根节点1处,每一次可以进行一下 ...

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

  9. Codeforces 914H Ember and Storm's Tree Game 【DP】*

    Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...

随机推荐

  1. python colorama模块

    colorama是一个python专门用来在控制台.命令行输出彩色文字的模块,可以跨平台使用. 1. 安装colorama模块 pip install colorama 可用格式常数: Fore: B ...

  2. <转>jmeter(十一)JDBC Request之Query Type

    本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...

  3. 自学Java第七周的总结

    这一周里我将看过的知识点又复习了一遍,下个星期打算将题做一遍

  4. 自写Jquery插件 Tab

    原创文章,转载请注明出处,谢谢!https://www.cnblogs.com/GaoAnLee/p/9067017.html 每每看到别人写的Jquery插件,自己也试着学习尝试,终有结果,废话不多 ...

  5. js 简易时钟

    html部分 <div id="clock"> </div> css部分 #clock{ width:600px ; text-align: center; ...

  6. 面试必问之JVM篇

    前言 只有光头才能变强 JVM在准备面试的时候就有看了,一直没时间写笔记.现在到了一家公司实习,闲的时候就写写,刷刷JVM博客,刷刷电子书. 学习JVM的目的也很简单: 能够知道JVM是什么,为我们干 ...

  7. JavaScript笔记 #08# 用Regex辅助生成文章目录 V2.0

    索引 简介 测试用例 代码 简介 * 用Regex辅助生成文章目录 2.0 * 1.提高了功能的通用性(假定的文章格式更加普遍,即按照h2h3h4分级) * 2.改善了代码的可读性(稍微牺牲了一点点性 ...

  8. mongodb安装、远程访问设置、基本常用操作和命令以及GUI

    https://www.mongodb.com/download-center?jmp=nav下载对应OS的版本,tar -xzvf解压 对于最新版本比如3.4,windows 7下可能回报api-m ...

  9. k8s tensorflow

    Online learning github source Kubeflow实战系列 Prepare 了解ksonnet初探Google之Kubeflow (采用的是0.8.0)install dep ...

  10. linux有趣的命令screen

    screen类似一个容器, 可以把当前前台运行的应用shell窗口关闭而不影响运行, 跟后后nohup有点相似, 不过我觉得比nohup还好用 用法1: screen 然后会弹出一个新的shell窗口 ...