Codeforces 570D - Tree Requests(树上启发式合并)
570D - Tree Requests
题意
给出一棵树,每个节点上有字母,查询 u k,问以 u 为根节点的子树下,深度为 k 的所有子节点上的字母经过任意排列是否能构成回文串。
分析
一个数组 \(C[i][j]\) 表示深度为 \(i\) 字母为 \(j\) 的数量,数组 \(odd[i]\) 表示深度为 \(i\) 时出现次数为奇数的字母种数。
如果想要构成回文串,那么某一深度下出现的次数为奇数的字母不能超过一种,注意如果字符串长度为 0 也叫回文串。
code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 5e5 + 10;
int n;
int fa[MAXN], son[MAXN], dep[MAXN], siz[MAXN];
int col[MAXN];
int cnt, head[MAXN];
struct Edge {
int to, next;
} e[MAXN << 1];
struct Ex {
int x, c;
};
vector<Ex> ex[MAXN];
void addedge(int u, int v) {
e[cnt].to = v; e[cnt].next = head[u]; head[u] = cnt++;
e[cnt].to = u; e[cnt].next = head[v]; head[v] = cnt++;
}
void dfs(int u) {
siz[u] = 1;
son[u] = 0;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].to != fa[u]) {
fa[e[i].to] = u;
dep[e[i].to] = dep[u] + 1;
dfs(e[i].to);
if(siz[e[i].to] > siz[son[u]]) son[u] = e[i].to;
siz[u] += siz[e[i].to];
}
}
}
int vis[MAXN], ans[MAXN];
int mk[MAXN];
int C[MAXN][30], odd[MAXN], num[MAXN];
void change(int u, int c) {
C[dep[u]][mk[u]] += c;
num[dep[u]] += c;
if(C[dep[u]][mk[u]] & 1) odd[dep[u]]++;
else odd[dep[u]]--;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].to != fa[u] && !vis[e[i].to]) change(e[i].to, c);
}
}
void dfs1(int u, int flg) {
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].to != fa[u] && e[i].to != son[u]) dfs1(e[i].to, 1);
}
if(son[u]) {
dfs1(son[u], 0);
vis[son[u]] = 1;
}
change(u, 1);
int sz = ex[u].size();
for(int i = 0; i < sz; i++) {
ans[ex[u][i].x] = (odd[ex[u][i].c] <= 1 || num[ex[u][i].c] == 0);
}
if(son[u]) vis[son[u]] = 0;
if(flg) change(u, -1);
}
char ss[MAXN];
int main() {
int m;
scanf("%d%d", &n, &m);
memset(head, -1, sizeof head);
cnt = 0;
dep[1] = 1;
for(int i = 2; i <= n; i++) {
int x;
scanf("%d", &x);
addedge(i, x);
}
scanf("%s", ss);
for(int i = 0; i < n; i++) {
mk[i + 1] = ss[i] - 'a';
}
dfs(1);
for(int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
ex[x].push_back(Ex{i, y});
}
dfs1(1, 0);
for(int i = 0; i < m; i++) {
puts(ans[i] ? "Yes" : "No");
}
return 0;
}
Codeforces 570D - Tree Requests(树上启发式合并)的更多相关文章
- Codeforces 570D TREE REQUESTS dfs序+树状数组 异或
http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...
- codeforces 375D . Tree and Queries 启发式合并 || dfs序+莫队
题目链接 一个n个节点的树, 每一个节点有一个颜色, 1是根节点. m个询问, 每个询问给出u, k. 输出u的子树中出现次数大于等于k的颜色的数量. 启发式合并, 先将输入读进来, 然后dfs完一个 ...
- Codeforces 570D TREE REQUESTS dfs序+树状数组
链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...
- Codeforces 570D - Tree Requests【树形转线性,前缀和】
http://codeforces.com/contest/570/problem/D 给一棵有根树(50w个点)(指定根是1号节点),每个点上有一个小写字母,然后有最多50w个询问,每个询问给出x和 ...
- CodeForces 570D - Tree Requests - [DFS序+二分]
题目链接:https://codeforces.com/problemset/problem/570/D 题解: 这种题,基本上容易想到DFS序. 然后,我们如果再把所有节点分层存下来,那么显然可以根 ...
- codeforces 570D.Tree Requests
[题目大意]: 给定一棵树,树的每个节点对应一个小写字母字符,有m个询问,每次询问以vi为根节点的子树中,深度为hi的所有节点对应的字符能否组成一个回文串: [题目分析]: 先画个图,可看出每次询问的 ...
- 总结-DSU ON TREE(树上启发式合并)
考试遇到一道题: 有一棵n个点的有根树,每个点有一个颜色,每次询问给定一个点\(u\)和一个数\(k\),询问\(u\)子是多少个不同颜色节点的\(k\)级祖先.n<=500000. 显然对每一 ...
- dsu on tree (树上启发式合并) 详解
一直都没出过算法详解,昨天心血来潮想写一篇,于是 dsu on tree 它来了 1.前置技能 1.链式前向星(vector 建图) 2.dfs 建树 3.剖分轻重链,轻重儿子 重儿子 一个结点的所有 ...
- 神奇的树上启发式合并 (dsu on tree)
参考资料 https://www.cnblogs.com/zhoushuyu/p/9069164.html https://www.cnblogs.com/candy99/p/dsuontree.ht ...
随机推荐
- 修改MySQL数据库字符集
Preface I've demonstrated how to change character set in Oracle database in my previous blog ...
- MQ消息中间件
MQ是什么? MQ是Message Queue消息队列的缩写.消息队列是一种应用程序对应用程序的通信方法.应用程序通过写和检索入列队的针对应用程序的数据(消息)来进行通信,而不需要专用连接来链接它们. ...
- Elasticsearch自定义分析器
关于分析器 ES中默认使用的是标准分析器(standard analyzer).如果需要对某个字段使用其他分析器,可以在映射中该字段下说明.例如: PUT /my_index { "mapp ...
- 网络--OSI七层模型详解
OSI 七层模型通过七个层次化的结构模型使不同的系统不同的网络之间实现可靠的通讯,因此其最主要的功能就是帮助不同类型的主机实现数据传输 . 完成中继功能的节点通常称为中继系统.在OSI七层模型中,处于 ...
- 移动平台自动化测试:appium(一)
Appium 是一个开源的,跨平台的自动化测试工具.它支持模拟器(iOS,FirefoxOS,Android)和真机(iOS, Android, FirefoxOS)上的原生应用,混合应用和移动 we ...
- CSS3 em && rem 详细教程
1 # mobile css & rem & em & px > 1 rem === 16px 任意浏览器的默认字体高都是 16px, 所有未经调整的浏览器都符合: 1e ...
- BZOJ 3052: [wc2013]糖果公园 | 树上莫队
题目: UOJ也能评测 题解 请看代码 #include<cstdio> #include<algorithm> #include<cstring> #includ ...
- ubuntu启动报错 Errors were found while checking the disk-drive for /
开机报这个错误,主要原因是硬盘检测不通过导致的,下面介绍两种方法规避该问题: 修改grub 这个方法网上比较多,直接贴过来: 进入Ubuntu启动菜单时,光标选中 *Ubuntu 后,按键盘上的 e ...
- 【CZY选讲·次大公因数】
题目描述 给定n个数ai,求sgcd(a1,a1),sgcd(a1,a2),…,sgcd(a1,an). 其中sgcd(x,y)表示x和y的次大公因数.若不存在次大公因数,sgcd(x,y)=-1 ...
- Linux命令(IT)
ls 查看当前目录下文件 cd 指定目录 sftp zygf@xxx.xxx.xxx.xxx 进行登录zygf用户 sftp命令行登录过程: ① sftp xxx.xxx.xxx.xxx ...