Codeforces Round #316 (Div. 2) D Tree Requests
官方题解是离线询问,dfs树形转线性,然后二分找区间。
还有一种比较好的做法是直接dfs,将当前访问这个结点u相关的询问之前的状态存起来,然后访问完以后利用异或开关性,得到这颗子树上的答案。
代码是学习别人的http://blog.csdn.net/squee_spoon/article/details/47666667
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+; char s[maxn]; int head[maxn],to[maxn],nxt[maxn],ecnt; void addEdge(int u,int v)
{
to[ecnt] = v;
nxt[ecnt] = head[u];
head[u] = ecnt++;
} int qhead[maxn],qnxt[maxn]; void init()
{
memset(head,-,sizeof(head));
memset(qhead,-,sizeof(qhead));
} struct Query
{
int h;
int other;
bool ans;
}query[maxn]; void addQuery(int v,int i)
{
qnxt[i] = qhead[v];
qhead[v] = i;
} bool ok(int n)
{
while(n){
if(n&){
return !(n>>);
}
n>>=;
}
return true;
}
int d[maxn];
int cnt[maxn];
void dfs(int u)
{
for(int i = qhead[u]; ~i; i = qnxt[i]){
query[i].other = cnt[query[i].h];
}
cnt[d[u]] ^= (<<(s[u]-'a'));
for(int i = head[u]; ~i; i = nxt[i]){
d[to[i]] = d[u]+;
dfs(to[i]);
}
for(int i = qhead[u]; ~i; i = qnxt[i]){
query[i].ans = ok(cnt[query[i].h]^query[i].other);
}
} int main()
{
init();
int n,m;
scanf("%d%d",&n,&m);
for(int i = ; i <= n; i++){
int fa; scanf("%d",&fa);
addEdge(fa,i);
}
scanf("%s",s+);
for(int i = ; i < m; i++){
int v; scanf("%d%d",&v,&query[i].h);
addQuery(v,i);
}
d[] = ;
dfs();
for(int i = ; i < m; i++){
query[i].ans?puts("Yes"):puts("No");
}
return ;
}
Codeforces Round #316 (Div. 2) D Tree Requests的更多相关文章
- 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 Round #316 (Div. 2) D. Tree Requests(dsu)
题目链接 题意:对于m次询问 求解以vi为根节点 深度为hi的的字母能不能组合成回文串. 思路:暴力dsu找一边 简直就是神技! #include<bits/stdc++.h> #defi ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #316 (Div. 2)
A. Elections time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树
C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...
- Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)
https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...
- Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】
任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...
随机推荐
- g2o待总结
http://blog.csdn.net/u010566411/article/details/53862601
- 【网络爬虫】【java】微博爬虫(三):庖丁解牛——HTML结构分析与正则切分
在上一篇文章中已经通过请求的url地址把html页面爬取下来了,这里分别以网易微博的html和新浪微博的html为例来分析如何提取微博数据. 一.网易微博解析 相比新浪微博的html结构,网易微博的比 ...
- HTML学习笔记(二)HTML格式化
很多标签都可以用来改变文本的外观,并为文本关联其隐藏的含义.总地来说,这些标签可以分成两类:基于内容的样式(content-based style)和物理样式(physical style). 一.基 ...
- Qt开篇
使用Qt两年有余,遇到问题多是现查现用,由于之前供职于一家保密性较强的单位,遇到的很多问题没有被记录下来.从今天开始,我会记记录自己的笔记.
- bzoj 5120: [2017国家集训队测试]无限之环【最小费用最大流】
玄妙的建图-- 这种平衡度数的题按套路是先黑白染色然后分别连ST点,相邻格子连黑向白连费用1流量0的边,然后考虑费用怎么表示 把一个点拆成五个,上下左右中,中间点黑白染色连ST, 对于连S的点,中点连 ...
- php-fpm 高并发 参数调整
工作中经常会遇到会给客户配置服务器,其中有的客户还会有并发量要求,其中也会必须要用负载均衡承载压力的.增加服务器数量肯定能有效的提升服务器承载能力,但只有根据目前已有配置设置好单台服务器才能更好的发挥 ...
- 一篇文章带你搞懂 SpringBoot与Swagger整合
Swagger使用由于不喜欢csdn的markwoen编辑器,对代码样式支持不好,看着不舒服,对审美要求比较高的同学移步github:https://github.com/itguang/swagge ...
- 51nod 1562 玻璃切割 (set)
#include<stdio.h> #include<iostream> #include<set> using namespace std; typedef lo ...
- [题解](树形dp/换根)小x游世界树
2. 小x游世界树 (yggdrasi.pas/c/cpp) [问题描述] 小x得到了一个(不可靠的)小道消息,传说中的神岛阿瓦隆在格陵兰海的某处,据说那里埋藏着亚瑟王的宝藏,这引起了小x的好奇,但当 ...
- UWP 切换语言
关于UWP切换语言的具体可以看这篇.http://www.cnblogs.com/hupo376787/p/7775291.html 这里我就记录一些自己的. 目前大多数软件用的都是利用文本资源文件来 ...