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\ 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的更多相关文章
- 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 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...
- Codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)
感觉dsu on tree一定程度上还是与点分类似的.考虑求出跨过每个点的最长满足要求的路径,再对子树内取max即可. 重排后可以变成回文串相当于出现奇数次的字母不超过1个.考虑dsu on tree ...
- Codeforces.741D.Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree 思路)
题目链接 \(Description\) 给定一棵树,每条边上有一个字符(a~v).对每个节点,求它的子树中一条最长的路径,满足 路径上所有边上的字符可以重新排列成一个回文串.输出其最长长度. \(n ...
- 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 思路: 树上启发式合并 从根节点出发到每个位置的每个字符的奇偶性记为每个位 ...
- 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 ...
- codeforces 1065F Up and Down the Tree
题目链接:codeforces 1065F Up and Down the Tree 题意:给出一棵树的节点数\(n\)以及一次移动的最大距离\(k\),现在有一个标记在根节点1处,每一次可以进行一下 ...
- 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 914H Ember and Storm's Tree Game 【DP】*
Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...
随机推荐
- Qt介绍1---QPA(Qt Platform Abstraction)
Qt是一个夸平台的库(一直宣称“Qt everywhere”),但是Qt底层不是夸平台的.比如:Qt中Gui部件的核心类QWidget,该类除了qwidget.h 和 qwidget.cpp两个原文件 ...
- ui-router .state参数配置
.state('页面被引用时的变量名',{ template: '<h1>My Contacts</h1>',//被应用时插入的模板,状态被激活时,它的模板会自动插入到父状态对 ...
- 使用Holer远程桌面登录家里电脑和公司内网电脑
1. Holer工具简介 Holer exposes local servers behind NATs and firewalls to the public internet over secur ...
- FFMPEG结构体分析:AVFrame(解码后的数据)
https://blog.csdn.net/jxcr1984/article/details/52766524 本文转自: http://blog.csdn.net/leixiaohua1020/ar ...
- IDEA之HttpServletRequest之报错解决方案
@Controller public class UserController { @RequestMapping("/selectUser") public String sel ...
- 使ipconfig命令结果更整洁
在windows下,使用ipconfig命令会出来很多内容,很多事ipv6隧道适配器的内容.而现在大部分人都还用不到ipv6,因此我们可以输入以下命令关闭ipv6隧道适配器,使命令结果更整洁. net ...
- $ORACLE_HOME/rdbms/demo示例安装
需要手工安装p13390677_112040_Linux-x86-64_6of7.zip,或者win32_11gR2_examples.zip.默认不包含. 从Oracle Database 12c ...
- mysql、oracle分库分表方案之sharding-jdbc使用(非demo示例)
选择开源核心组件的一个非常重要的考虑通常是社区活跃性,一旦项目团队无法进行自己后续维护和扩展的情况下更是如此. 至于为什么选择sharding-jdbc而不是Mycat,可以参考知乎讨论帖子https ...
- java Condition条件变量的通俗易懂解释、基本使用及注意点
最近在看pthread方面的书,看到条件变量一节的时候,回忆了下java中条件变量的使用方式. java中条件变量都实现了java.util.concurrent.locks.Condition接口, ...
- php5.3.x连接MS SQL server2008
开篇 因为毕设老师需求的原因,虚拟旅游网站要求的数据库必须使用MS SQL server. 我最擅长的web编程语言是PHP,但是在PHP中链接MS SQL server是一件非常麻烦的事,我个人分析 ...