CF 570 D. Tree Requests
D. Tree Requests
http://codeforces.com/problemset/problem/570/D
题意:
一个以1为根的树,每个点上有一个字母(a-z),每次询问一个子树内深度为h的点是否可以构成回文串。(深度是到1的深度,没有也算,空回文串)
分析:
dsu on tree。询问子树信息。
判断是否构成回文:出现奇数次的字符小于等于1个。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
#include<set>
#include<vector>
#include<queue>
#include<map>
#define pa pair<int,int>
#define mp(a,b) make_pair(a,b)
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; int head[N], nxt[N], to[N], En;
int fa[N], siz[N], son[N], deth[N], ans[N], cnt[N][], ch[N];
char s[N];
vector< pa > q[N]; void add_edge(int u,int v) {
++En; to[En] = v; nxt[En] = head[u]; head[u] = En;
} void dfs(int u,int fa) {
siz[u] = ;
deth[u] = deth[fa] + ;
for (int i=head[u]; i; i=nxt[i]) {
int v = to[i];
dfs(v, u);
siz[u] += siz[v];
if (!son[u] || siz[son[u]] < siz[v]) son[u] = v;
}
} void add(int u) {
cnt[deth[u]][ch[u]] ++;
}
void Calc(int u) {
add(u);
for (int i=head[u]; i; i=nxt[i]) Calc(to[i]);
}
void Clear(int u) {
cnt[deth[u]][ch[u]] --;
for (int i=head[u]; i; i=nxt[i]) Clear(to[i]);
} void solve(int u,bool c) {
for (int i=head[u]; i; i=nxt[i])
if (to[i] != son[u]) solve(to[i], );
if (son[u]) solve(son[u], ); for (int i=head[u]; i; i=nxt[i])
if (to[i] != son[u]) Calc(to[i]);
add(u); for (int i=,sz=q[u].size(); i<sz; ++i) {
int flag = , id = q[u][i].second, h = q[u][i].first;
for (int j=; j<; ++j) if (cnt[h][j] & ) flag ++;
ans[id] = (flag <= );
} if (!c) Clear(u);
} int main() {
int n = read(), Q = read();
for (int i=; i<=n; ++i) {
int u = read();
add_edge(u, i);
}
scanf("%s",s + );
for (int i=; i<=n; ++i) ch[i] = s[i] - 'a';
for (int i=; i<=Q; ++i) {
int v = read(), h = read();
q[v].push_back(mp(h, i));
}
dfs(, );
solve(, );
for (int i=; i<=Q; ++i) puts(ans[i] ? "Yes" : "No");
return ;
}
CF 570 D. Tree Requests的更多相关文章
- codeforces 570 D. Tree Requests (dfs)
题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- codeforces 570 D Tree Requests
题意:给出一棵树.每一个结点都有一个字母,有非常多次询问,每次询问.以结点v为根的子树中高度为h的后代是否可以经过调整变成一个回文串. 做法: 推断能否够构成一个回文串的话,仅仅须要知道是否有大于一个 ...
- Codeforces 570D TREE REQUESTS dfs序+树状数组 异或
http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...
- Codeforces Round #316 (Div. 2) D. Tree Requests dfs序
D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces 570D - Tree Requests(树上启发式合并)
570D - Tree Requests 题意 给出一棵树,每个节点上有字母,查询 u k,问以 u 为根节点的子树下,深度为 k 的所有子节点上的字母经过任意排列是否能构成回文串. 分析 一个数组 ...
- Codeforces 570D TREE REQUESTS dfs序+树状数组
链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...
- CF 570D. Tree Requests [dsu on tree]
传送门 题意: 一棵树,询问某棵子树指定深度的点能否构成回文 当然不用dsu on tree也可以做 dsu on tree的话,维护当前每一个深度每种字母出现次数和字母数,我直接用了二进制.... ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- 虚拟机装ubuntu (kylin) 时常遇问题
在想保留Windows操作系统的同时还能用Linux进行学习和开发,安装双系统或安装虚拟机都是不错的选择,依个人情况而定. (P.S:此行留给后期会写的双系统博客) 这篇文章收集了自己在用VMware ...
- Codeforces Round #437 (Div. 2)[A、B、C、E]
Codeforces Round #437 (Div. 2) codeforces 867 A. Between the Offices(水) 题意:已知白天所在地(晚上可能坐飞机飞往异地),问是否从 ...
- 打印出类所在的jar包
ackage time; /** * Created by sheting on 10/20/2017 */ public class Test { public static void main(S ...
- BIND简易教程(3):DNSSec配置
目录:BIND简易教程(1):安装及基本配置BIND简易教程(2):BIND视图配置BIND简易教程(3):DNSSec配置 (本篇) DNSSec,有个半英半中的名字叫DNS安全扩展.说的好听一点, ...
- poj 3253 Fence Repair (STL优先队列)
版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/34805369 转载请注明出 ...
- windows自定义快捷键功能
如下:
- 关闭 XXXXX 前你必须关闭所有会话框
这个问题应该是和Microsoft管理控制台(Microsoft Management Console,MMC)有关系 我是在使用 任务计划程序 时碰到这个问题的,网上也有其他人在使用 事件查看器 的 ...
- __future__模块
Python提供了__future__模块,把下一个新版本的特性导入到当前版本,于是我们就可以在当前版本中使用一些新版本的特性,比如除法: 在Python 2.x中,对于除法有两种情况,如果是整数相除 ...
- c#类中字段和方法中变量的声明问题
字段和局部变量的作用域冲突 某些情况下可以区分名称相同,作用域相同的两个标识符.原因是C#在变量之间有一个基本的区分,它把在类级别声明的变量看作 字段,而把在方法中声明的变量看作局部变量. class ...
- 创建blob地址
aa="121" "121" b=new Blob([aa]) Blob(3) {size: 3, type: ""} window.URL ...