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(树上启发式合并)的更多相关文章

  1. Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

    http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...

  2. codeforces 375D . Tree and Queries 启发式合并 || dfs序+莫队

    题目链接 一个n个节点的树, 每一个节点有一个颜色, 1是根节点. m个询问, 每个询问给出u, k. 输出u的子树中出现次数大于等于k的颜色的数量. 启发式合并, 先将输入读进来, 然后dfs完一个 ...

  3. Codeforces 570D TREE REQUESTS dfs序+树状数组

    链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...

  4. Codeforces 570D - Tree Requests【树形转线性,前缀和】

    http://codeforces.com/contest/570/problem/D 给一棵有根树(50w个点)(指定根是1号节点),每个点上有一个小写字母,然后有最多50w个询问,每个询问给出x和 ...

  5. CodeForces 570D - Tree Requests - [DFS序+二分]

    题目链接:https://codeforces.com/problemset/problem/570/D 题解: 这种题,基本上容易想到DFS序. 然后,我们如果再把所有节点分层存下来,那么显然可以根 ...

  6. codeforces 570D.Tree Requests

    [题目大意]: 给定一棵树,树的每个节点对应一个小写字母字符,有m个询问,每次询问以vi为根节点的子树中,深度为hi的所有节点对应的字符能否组成一个回文串: [题目分析]: 先画个图,可看出每次询问的 ...

  7. 总结-DSU ON TREE(树上启发式合并)

    考试遇到一道题: 有一棵n个点的有根树,每个点有一个颜色,每次询问给定一个点\(u\)和一个数\(k\),询问\(u\)子是多少个不同颜色节点的\(k\)级祖先.n<=500000. 显然对每一 ...

  8. dsu on tree (树上启发式合并) 详解

    一直都没出过算法详解,昨天心血来潮想写一篇,于是 dsu on tree 它来了 1.前置技能 1.链式前向星(vector 建图) 2.dfs 建树 3.剖分轻重链,轻重儿子 重儿子 一个结点的所有 ...

  9. 神奇的树上启发式合并 (dsu on tree)

    参考资料 https://www.cnblogs.com/zhoushuyu/p/9069164.html https://www.cnblogs.com/candy99/p/dsuontree.ht ...

随机推荐

  1. C++文件操作(转)

    C++文件操作(转) 第一个:简单,容易理解,常用:http://www.cnblogs.com/uniqueliu/archive/2011/08/03/2126545.html 第二个:详细的,如 ...

  2. Eclipse 出现“polling news feeds”的解决办法

    小编突然心血来潮,安装了一下Java的环境,eclipse的IDE来写点Java,但是是不是出现以下的弹窗,实在是闹心,后来网上看了前辈们的解决办法,特此记录一下.如有侵权,敬请告知!!! 1. 找到 ...

  3. 机器学习框架Tensorflow数字识别MNIST

    SoftMax回归  http://ufldl.stanford.edu/wiki/index.php/Softmax%E5%9B%9E%E5%BD%92 我们的训练集由  个已标记的样本构成: ,其 ...

  4. [C++] 数据结构应用——链表

    C++ 数据结构应用--链表 代码已经封装成class啦,方便使用. 头文件:Linklist.h #include <iostream> /*********************** ...

  5. UVa 1445 - Cubist Artwork

    统计正面看高度为i的竖条个数为cnt1[i], 统计侧面看高度为i的竖条个数为cnt2[i]: ans = sum( i * max( cnt1[i], cnt2[i] ) ); ( 1 <= ...

  6. nyoj 题目21 三个水杯

    三个水杯 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有 ...

  7. python os操作

    大家先看一下Python os模块中的部分函数 python 路径相关的函数 os.listdir(dirname):列出dirname下的目录和文件 os.getcwd():获得当前工作目录 os. ...

  8. HTML5的JavaScript选择器介绍

    在HTML5出现之前使用JavaScript查找DOM元素,有以下三种原生的方法: getElementById:根据指定元素的id属性返回元素 getElementsByName:返回所有指定nam ...

  9. 火焰图还有perf

    http://www.brendangregg.com/flamegraphs.html zhangyichun大神的systemtap脚本: https://github.com/openresty ...

  10. 2017 多校4 Security Check

    2017 多校4 Security Check 题意: 有\(A_i\)和\(B_i\)两个长度为\(n\)的队列过安检,当\(|A_i-B_j|>K\)的时候, \(A_i和B_j\)是可以同 ...