传送门

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. 2019年7月22日A股科创板开板首日行情思考

    2019年7月22日A股科创板开板首日行情思考 原因:2019科创板开板交易 盘面:科创板交易活跃,首批上市25只股票大涨,最高达5倍涨幅:主板交投低迷,量能萎缩,大部分股票下跌. 操作:加仓 西安银 ...

  2. Codeforces Round #620 (Div. 2) A. Two Rabbits

    Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a p ...

  3. 「Luogu P5368 [PKUSC2018]真实排名」

    PKUSC签到题 题目大意 给出一个长度为 \(N\) 的序列,序列中有 \(K\) 个数会乘二,对于每个数计算在乘二后大于等于这个数的个数与乘二前没有发生变化的方案数. 分析 思路很清晰,可以将答案 ...

  4. excel表格 函数功能

    1.去重复 选中一个区域——>数据——>删除重复项 2.条件求和 按照条件筛选:筛选出一样的类目,将对应的值求和. =sumif(A$1:A$10,B2,C$1:C$10) A$1:A$1 ...

  5. [读书]The Man Who Solved the Market

    出乎个人意料的是,西蒙斯是从FICC类品种起步的,包括量化投资方法获得第一次重大突破也是在FICC品种上. FICC市场的深度不够,所以文艺复兴科技实现规模扩张是股票策略成功之后的事情,很靠后. 虽然 ...

  6. spring boot 动态注入bean

    方法一 SpringContextUtil public class SpringContextUtil { private static ApplicationContext application ...

  7. 最全Python学习路线图【2020最新版】

    2020年最新的python学习大纲,专为python高薪打造另外很多人在学习Python的过程中,往往因为没有好的教程或者没人指导从而导致自己容易放弃,为此我建了个Python交流.裙 :一久武其而 ...

  8. 金币(0)<P2015_1>

    金币 (coin.cpp/c/pas) [问题描述]  国王将金币作为工资,发放给忠诚的骑士.第一天,骑士收到一枚金币:之后两天(第二天和第三天),每天收到两枚金币:之后三天(第四.五.六天),每天收 ...

  9. PAT T1017 The Best Peak Shape

    动态规划找最长上升子序列,正反遍历一遍序列即可~ #include<bits/stdc++.h> using namespace std; ; int N; int a[maxn]; in ...

  10. git pull解决冲突

    git报错:Please commit your changes or stash them before you merge. 解决:1.不需要保留本地修改的话,直接将有冲突的文件还原再pull:g ...