题目链接: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. [转载]ORA-01502错误成因和解决方法

    检查一下索引状态,我们会注意到索引已经是“UNUSABLE”了.SQL> select index_name,index_type,tablespace_name,table_type,stat ...

  2. Hadoop学习笔记之三:DataNode

    DataNode对ClientDatanodeProtocol.InterDatanodeProtocol两个协议接口进行了实现,通过ipc::Server向Client.其它DN提供RPC服务(参见 ...

  3. jquery 页面分页的实现

      <!DOCTYPE html> <html> <head> <title>分页</title> <link rel="s ...

  4. GUID生成函数

    /** * GUID生成函数 * @return string */function create_guid() { $charid = strtoupper(md5(uniqid(mt_rand() ...

  5. docker 初步使用

    CentOS Linux release 7.2.1511 Docker version 17.03.1-ce 安装与启动 yum直接安装的docker版本较低,推荐这样安装: # 官方,可能网络连不 ...

  6. 【题解】Luogu UVA12345 Dynamic len(set(a[L:R]))

    原题传送门 这题要用动态莫队,我博客里有介绍 这道题和luogu P1903 [国家集训队]数颜色 / 维护队列差不多,解法就在上面那篇博客里qaq 主要的问题是如何排序? 排序有三个关键字: 1.左 ...

  7. 【python37--面向对象】

    一. self是什么 绑定方法,self就是实例对象的唯一标志 >>> class Ball: def setName(self,name): self.name = name de ...

  8. 【转】RHEL(RedHat Enterprise Linux)5/6 ISO镜像下载

    本文贴出了RHEL(RedHat Enterprise Linux)发行版本中常用的服务器版本的ISO镜像文件,供大家下载学习使用,贴出的版本有RedHat Enterprise Linux(RHEL ...

  9. orm查询存在价格为空问题

    明明写的没错还是查不到 打印一下sql语句: 解决办法: 把数字变成字符串格式 所以涉及金融计算,涉及小数啊,要求特别精确的,我们用字符串存储.

  10. Mybatis的in使用

    foreach元素的属性主要有 item,index,collection,open,separator,close.    item表示集合中每一个元素进行迭代时的别名,    index指 定一个 ...