「CF741D」Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
传送门
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的更多相关文章
- 「CF1039D」You Are Given a Tree
传送门 Luogu 解题思路 整体二分. 的确是很难看出来,但是你可以发现输出的答案都是一些可以被看作是关键字处于 \([1, n]\) 的询问,而答案的范围又很显然是 \([0, n]\),这不就刚 ...
- 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的 ...
- LoibreOJ 2042. 「CQOI2016」不同的最小割 最小割树 Gomory-Hu tree
2042. 「CQOI2016」不同的最小割 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- 「BZOJ2654」tree
「BZOJ2654」tree 最小生成树+二分答案. 最开始并没有觉得可以二分答案,因为答案并不单调啊. 其实根据题意,白边的数目肯定大于need条,而最小生成树的白边数并不等于need(废话),可以 ...
- 【题解】#6622. 「THUPC 2019」找树 / findtree(Matrix Tree+FWT)
[题解]#6622. 「THUPC 2019」找树 / findtree(Matrix Tree+FWT) 之前做这道题不理解,有一点走火入魔了,甚至想要一本近世代数来看,然后通过人类智慧思考后发现, ...
- 「SPOJ10707」Count on a tree II
「SPOJ10707」Count on a tree II 传送门 树上莫队板子题. 锻炼基础,没什么好说的. 参考代码: #include <algorithm> #include &l ...
- 「SPOJ1487」Query on a tree III
「SPOJ1487」Query on a tree III 传送门 把树的 \(\text{dfs}\) 序抠出来,子树的节点的编号位于一段连续区间,然后直接上建主席树区间第 \(k\) 大即可. 参 ...
- 「luogu2633」Count on a tree
「luogu2633」Count on a tree 传送门 树上主席树板子. 每个节点的根从其父节点更新得到,查询的时候差分一下就好了. 参考代码: #include <algorithm&g ...
- 「AGC035C」 Skolem XOR Tree
「AGC035C」 Skolem XOR Tree 感觉有那么一点点上道了? 首先对于一个 \(n\),若 \(n\equiv 3 \pmod 4\),我们很快能够构造出一个合法解如 \(n,n-1, ...
随机推荐
- Executor、Executors、ExecutorService多线程操作
Executor:一个接口,其定义了一个接收Runnable对象的方法executor,其方法签名为executor(Runnable command),该方法接收一个Runable实例,它用来执行一 ...
- BGP前缀过滤(正则表达式)
BGP的正则表达式一般用在as-path中,常用的如下: .(点):表示匹配任意一个字符,包括空格. *:表示匹配零个或多个模式的出现.即前一个字符出现0次或多次. +:表示匹配一个或多个模式的出现. ...
- 2016-2017学年第三次测试赛 习题H MCC的考验
问题 H: MCC的考验 时间限制: 1 Sec 内存限制: 128 MB 题目描述 MCC男神听说新一期的选拔赛要开始了,给各位小伙伴们带来了一道送分题,如果你做不出来,MCC会很伤心的. 给定一 ...
- Python语言——map/reduce的用法
Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Processing on Large Clus ...
- C语言-断言
1 作用: 断言常做语言处理的高级形式,自动处理软件隐藏很深其且它手段不易发现的错误,快速进行异常定位.同时这也是软件单元测试必须的技术. 2 使用范围: 2.1放在函数入口对入口参数进行合法性检查( ...
- DNS域名解析服务(重点)
一 .DNS 系统的作用 1.DNS 服务器概述 DNS 系统在网络中的作用就是维护着一个地址数据库,其中记录了各种主机域名:与 IP地址的对应关系,以便为客户程序提供正向或反向的地址查询服务,即正 ...
- redhat7.6 DNS配置正向解析
1.安装DNS服务 yum install bind yum install bind-chroot 安装完的配置文件/etc/named.conf 启动systemctl start named.s ...
- leetCode练题——12. Integer to Roman
1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- 【JAVA算法题】职业抢劫
题目 /*You are a professional robber planning to rob houses along a street. * Each house has a certain ...
- Struts2.0笔记二
Mvc与servlet 1.1 Servlet的优点 1. 是mvc的基础,其他的框架比如struts1,struts2,webwork都是从servlet基础上发展过来的.所以掌握servle ...