传送门

Luogu

解题思路

考虑把22个字符状压下来,易知合法情况就是状态中之多有一个1,这个可以暴力一点判断23次。

然后后就是 dsu on the tree 了。

细节注意事项

  • 咕咕咕

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
} const int _ = 500010; int tot, head[_], nxt[_ << 1], ver[_ << 1], w[_ << 1];
inline void Add_edge(int u, int v, int d)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, w[tot] = d; } int n, dis[_], tg[1 << 22 | 2];
int dep[_], siz[_], son[_], ans[_];
int num, L[_], R[_], id[_]; inline void dfs(int u, int f) {
siz[u] = 1, dep[u] = dep[f] + 1;
L[u] = ++num, id[num] = u;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == f) continue;
dis[v] = dis[u] ^ w[i];
dfs(v, u), siz[u] += siz[v];
if (siz[v] > siz[son[u]]) son[u] = v;
}
R[u] = num;
} inline void dfss(int u, int f, int keep) {
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == f || v == son[u]) continue;
dfss(v, u, 0), ans[u] = max(ans[u], ans[v]);
}
if (son[u]) dfss(son[u], u, 1), ans[u] = max(ans[u], ans[son[u]]);
if (tg[dis[u]]) ans[u] = max(ans[u], tg[dis[u]] - dep[u]);
for (rg int k = 0; k < 22; ++k)
if (tg[dis[u] ^ (1 << k)])
ans[u] = max(ans[u], tg[dis[u] ^ (1 << k)] - dep[u]);
tg[dis[u]] = max(tg[dis[u]], dep[u]);
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == son[u] || v == f) continue;
for (rg int x = L[v]; x <= R[v]; ++x) {
int p = id[x];
if (tg[dis[p]]) ans[u] = max(ans[u], tg[dis[p]] + dep[p] - 2 * dep[u]);
for (rg int k = 0; k < 22; ++k)
if (tg[dis[p] ^ (1 << k)]) ans[u] = max(ans[u], tg[dis[p] ^ (1 << k)] + dep[p] - 2 * dep[u]);
}
for (rg int x = L[v]; x <= R[v]; ++x)
tg[dis[id[x]]] = max(tg[dis[id[x]]], dep[id[x]]);
}
if (!keep) for (rg int x = L[u]; x <= R[u]; ++x) tg[dis[id[x]]] = 0;
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n);
for (rg int p, i = 2; i <= n; ++i) {
read(p); char c = getchar();
while (!isalpha(c)) c = getchar();
Add_edge(p, i, 1 << (c - 'a'));
Add_edge(i, p, 1 << (c - 'a'));
}
dfs(1, 0), dfss(1, 0, 1);
for (rg int i = 1; i <= n; ++i)
printf("%d%c", ans[i], " \n"[i == n]);
return 0;
}

完结撒花 \(qwq\)

「CF741D」Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths的更多相关文章

  1. 「CF1039D」You Are Given a Tree

    传送门 Luogu 解题思路 整体二分. 的确是很难看出来,但是你可以发现输出的答案都是一些可以被看作是关键字处于 \([1, n]\) 的询问,而答案的范围又很显然是 \([0, n]\),这不就刚 ...

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

  3. LoibreOJ 2042. 「CQOI2016」不同的最小割 最小割树 Gomory-Hu tree

    2042. 「CQOI2016」不同的最小割 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据   题目描述 ...

  4. 「BZOJ2654」tree

    「BZOJ2654」tree 最小生成树+二分答案. 最开始并没有觉得可以二分答案,因为答案并不单调啊. 其实根据题意,白边的数目肯定大于need条,而最小生成树的白边数并不等于need(废话),可以 ...

  5. 【题解】#6622. 「THUPC 2019」找树 / findtree(Matrix Tree+FWT)

    [题解]#6622. 「THUPC 2019」找树 / findtree(Matrix Tree+FWT) 之前做这道题不理解,有一点走火入魔了,甚至想要一本近世代数来看,然后通过人类智慧思考后发现, ...

  6. 「SPOJ10707」Count on a tree II

    「SPOJ10707」Count on a tree II 传送门 树上莫队板子题. 锻炼基础,没什么好说的. 参考代码: #include <algorithm> #include &l ...

  7. 「SPOJ1487」Query on a tree III

    「SPOJ1487」Query on a tree III 传送门 把树的 \(\text{dfs}\) 序抠出来,子树的节点的编号位于一段连续区间,然后直接上建主席树区间第 \(k\) 大即可. 参 ...

  8. 「luogu2633」Count on a tree

    「luogu2633」Count on a tree 传送门 树上主席树板子. 每个节点的根从其父节点更新得到,查询的时候差分一下就好了. 参考代码: #include <algorithm&g ...

  9. 「AGC035C」 Skolem XOR Tree

    「AGC035C」 Skolem XOR Tree 感觉有那么一点点上道了? 首先对于一个 \(n\),若 \(n\equiv 3 \pmod 4\),我们很快能够构造出一个合法解如 \(n,n-1, ...

随机推荐

  1. PAT 1007 Maximum Subsequence Sum (最大连续子序列之和)

    Given a sequence of K integers { N1, N2, ..., *N**K* }. A continuous subsequence is defined to be { ...

  2. 吴裕雄--天生自然Numpy库学习笔记:NumPy 字符串函数

    这些函数在字符数组类(numpy.char)中定义. add() 对两个数组的逐个字符串元素进行连接 multiply() 返回按元素多重连接后的字符串 center() 居中字符串 capitali ...

  3. jenkins windows 2.204版,免安装,推荐插件齐备.

    windows专用,已安装好推荐插件, 更新了安装源为清华源,也就是说只要官方的插件,你都可以秒速下载了.香不? 解压到一个文件夹,管理员模式运行cmdcd 文件夹名jenkins install这样 ...

  4. 「JSOI2014」打兔子

    「JSOI2014」打兔子 传送门 首先要特判 \(k \ge \lceil \frac{n}{2} \rceil\) 的情况,因为此时显然可以消灭所有的兔子,也就是再环上隔一个点打一枪. 但是我们又 ...

  5. Vagrant 安装使用

    先安装虚拟机 https://www.virtualbox.org/ 再安装 https://www.vagrantup.com/  1.nginxhttp://nginx.org/download/ ...

  6. 1123. Lowest Common Ancestor of Deepest Leaves

    link to problem Description: Given a rooted binary tree, return the lowest common ancestor of its de ...

  7. Plastic Bottle Manufacturer - What Is The Direction Of Plastic Bottles?

    Plastic bottle manufacturers explain: protective packaging for cosmetic cleaning products requires b ...

  8. 3 HTML标题&元素&图像&属性&字体增强&链接&头部标签与元素

    HTML标题(heading) 通过<h1>~~~<h6>定义,每个元素代表文档中不同级别的内容. h1表示主标题,the main heading , h2,3分别表示二级. ...

  9. 汇编语言从入门到精通-5微机CPU的指令系统1

    微机CPU的指令系统 5.1 汇编语言指令格式 为了介绍指令系统中指令的功能,先要清楚汇编语言是如何书写指令的,这就象在学习高级语言程序设计时,要清楚高级语言语句的语义.语法及其相关规定一样. 5.1 ...

  10. 第1节 Scala基础语法:13、list集合的定义和操作;16、set集合;17、map集合

    list.+:5 , list.::5: 在list集合头部添加单个元素5 : li1.:+(5):在list集合尾部添加单个元素5: li1++li2,li1:::li2:在li1集合尾部添加il2 ...