Codeforces Round #383 D
传送门
Description
Arpa has a rooted tree (connected acyclic graph) consisting of n vertices. The vertices are numbered 1 through n, the vertex 1 is the root. There is a letter written on each edge of this tree. Mehrdad is a fan of Dokhtar-kosh things. He call a string Dokhtar-kosh, if we can shuffle the characters in string such that it becomes palindrome.
He asks Arpa, for each vertex v, what is the length of the longest simple path in subtree of v that form a Dokhtar-kosh string.
题意:一棵根为\(1\) 的树,每条边上有一个字符(\(a-v\)共\(22\)种)。 一条简单路径被称为\(Dokhtar-kosh\)当且仅当路径上的字符经过重新排序后可以变成一个回文串。 求每个子树中最长的\(Dokhtar-kosh\)路径的长度。
Solution
所求的路径其实就是只有\(0/1\)个字符出现奇数次的字符串
发现最多只有\(22\)种字符,所以可以把一个点到根路径上字符的出现情况压成一个二进制数\(val_i\)
一个数位\(i\)为\(1\)表示路径上编号\(i\)的字符出现了奇数次
所以一条路径合法等价于\(val_a \ \ xor \ \ val_b=0 \ \ or \ \ 2^i\)
发现是静态的子树信息维护,可以采用\(dsu\ on \ tree\)
每个点只计算以这个点为\(LCA\)的最长路径,可以一个一个子树合并进来
维护一个\(mxdep_i\),表示\(val_x=i\)的最大的\(dep_x\)
对于\(dsu\ on \ tree\):
如果一个关于子树集合的询问满足
往一个集合里插入一个点是\(O(1)\)的
删除元素都是到空为止,且每个点的删除为\(O(1)\)
那么对于这个询问就可以做到\(O(nlog_2n)\)
因为每个点只会在轻边处被删除,因而最多被删除\(O(log_2n)\)次
Code
#include<bits/stdc++.h>
#define ll long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)>(b)?(b):(a))
#define reg register
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}
const int MN=5e5+5;
struct edge{int to,w,nex;}e[MN];
int n,en,hr[MN];
inline void ins(int x,int y,int w){e[++en]=(edge){y,w,hr[x]};hr[x]=en;}
int val[MN],dep[MN],mx[MN],siz[MN];
void dfs1(int x)
{
siz[x]=1;reg int i;
for(i=hr[x];i;i=e[i].nex)
val[e[i].to]=val[x]^(1<<e[i].w),
dep[e[i].to]=dep[x]+1,
dfs1(e[i].to),siz[x]+=siz[e[i].to],(siz[e[i].to]>siz[mx[x]])?mx[x]=e[i].to:0;
}
#define ri reg int i
int cx,mx_dep[1<<22],ans[MN],Dec;
void _cal(int x)
{
if(mx_dep[val[x]]) cx=max(cx,dep[x]+mx_dep[val[x]]-Dec);
for(ri=0;i<22;++i)if(mx_dep[val[x]^(1<<i)])cx=max(cx,mx_dep[val[x]^(1<<i)]+dep[x]-Dec);
}
void _upd(int x){mx_dep[val[x]]=max(dep[x],mx_dep[val[x]]);}
void cal(int x){_cal(x);for(ri=hr[x];i;i=e[i].nex)cal(e[i].to);}
void upd(int x){_upd(x);for(ri=hr[x];i;i=e[i].nex)upd(e[i].to);}
void clr(int x){mx_dep[val[x]]=0;for(ri=hr[x];i;i=e[i].nex)clr(e[i].to);}
void dfs2(int x,bool kep=0)
{
reg int i;
for(i=hr[x];i;i=e[i].nex) if(e[i].to!=mx[x]) dfs2(e[i].to);
if(mx[x]) dfs2(mx[x],1);Dec=dep[x]<<1;
for(i=hr[x];i;i=e[i].nex)cx=max(ans[e[i].to],cx);
for(i=hr[x];i;i=e[i].nex)if(e[i].to^mx[x])cal(e[i].to),upd(e[i].to);
_cal(x);_upd(x);ans[x]=cx;if(!kep)clr(x),cx=0;
}
int main()
{
n=read();reg int i,x;
for(i=2;i<=n;++i)x=read(),ins(x,i,getchar()-'a');
dfs1(1);dfs2(1);
for(i=1;i<=n;++i) printf("%d ",ans[i]);
return 0;
}
Blog来自PaperCloud,未经允许,请勿转载,TKS!
Codeforces Round #383 D的更多相关文章
- Codeforces Round #383 (Div. 2) 题解【ABCDE】
Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...
- Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环
题目链接:http://codeforces.com/contest/742/problem/C C. Arpa's loud Owf and Mehrdad's evil plan time lim ...
- Codeforces Round #383 (Div. 2) A,B,C,D 循环节,标记,暴力,并查集+分组背包
A. Arpa’s hard exam and Mehrdad’s naive cheat time limit per test 1 second memory limit per test 256 ...
- dfs + 最小公倍数 Codeforces Round #383 (Div. 2)
http://codeforces.com/contest/742/problem/C 题目大意:从x出发,从x->f[x] - > f[f[x]] -> f[f[f[x]]] -& ...
- 01背包dp+并查集 Codeforces Round #383 (Div. 2)
http://codeforces.com/contest/742/problem/D 题目大意:有n个人,每个人有重量wi和魅力值bi.然后又有m对朋友关系,朋友关系是传递的,如果a和b是朋友,b和 ...
- Codeforces Round #383 Div 1题解
第一次打Div 1,感觉还是挺难的..把基础题打完就日常划水了.... [A. Arpa's loud Owf and Mehrdad's evil plan](http://codeforces.c ...
- Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan
C. Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 me ...
- Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)
题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
- Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)
题目链接:http://codeforces.com/contest/742/problem/D D. Arpa's weak amphitheater and Mehrdad's valuable ...
- Codeforces Round #383 (Div. 2) B. 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 so ...
随机推荐
- JavaScript字符串Format
一直用C#编程,在日常字符串拼接中string.Format()一直是个很好用很常用的方法,不用自己+++,既影响开发效率也影响可读性 然而在js中并没有这样的函数可供使用,so整理了一个js的字符串 ...
- 利用ABAP 740的新关键字REDUCE完成一个实际工作任务
ABAP 740从2013年发布至今已经过去很长的时间了,下面这张图来自SAP社区博客: ABAP News for Release 7.40 – What is ABAP 7.40? 图中的ABAP ...
- 基于335X平台Linux交换芯片驱动开发
基于335X平台Linux交换芯片驱动开发 一.软硬件平台资料 1.开发板:创龙AM3359核心板,网口采用RMII形式. 2.Kernel版本:4.4.12,采用FDT 3.交换芯片MARVEL ...
- zabbix 自动发现(LLD)
概述 自动发现(LLD)提供了一种在计算机上为不同实体自动创建监控项,触发器和图形的方法.例如,Zabbix可以在你的机器上自动开始监控文件系统或网络接口,而无需为每个文件系统或网络接口手动创建监控项 ...
- abp vNext微服务框架分析
本文转载自:https://www.cnblogs.com/william-xu/p/11245738.html abp vNext新框架的热度一直都很高,于是最近上手将vNext的微服务Demo做了 ...
- 如何通过cmd获取到域名下的ip地址?例如获取百度的域名
百度首页的IP地址为[119.75.217.109] 你可以通过电脑本机进行查询,查询步骤如下: 1.点击[开始]--->>[运行],输入[cmd]: 按键盘上的[Win键]+[R键],调 ...
- 01 浅谈c++及面向对象编程
参考链接: 学习完c++但是对c++面向对象编程还是比较模糊,现在花时间总体来总结一下: c++中的对象是使用类来定义的,下面先重点讲一下类的概念. 说到类就要先说一下类的三种特性:封装,继承,多态. ...
- 大数据之路week07--day05 (一个基于Hadoop的数据仓库建模工具之一 HIve)
什么是Hive? 我来一个短而精悍的总结(面试常问) 1:hive是基于hadoop的数据仓库建模工具之一(后面还有TEZ,Spark). 2:hive可以使用类sql方言,对存储在hdfs上的数据进 ...
- redis 缓存问题,转载:https://www.cnblogs.com/liangsonghua/p/www_liangsonghua_me_22.html
缓存穿透: 缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时被动写的,并且处于容错考虑,如果从存储层查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失去了缓存的意义 ...
- 修改Windows10 命令终端cmd的编码为UTF-8
1. 临时修改 进入cmd窗口后,直接执行 chcp 2. 永久修改 在运行中输入regedit,找到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Pro ...
