官方题解是离线询问,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的更多相关文章

  1. 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 ...

  2. Codeforces Round #316 (Div. 2) D. Tree Requests(dsu)

    题目链接 题意:对于m次询问 求解以vi为根节点 深度为hi的的字母能不能组合成回文串. 思路:暴力dsu找一边 简直就是神技! #include<bits/stdc++.h> #defi ...

  3. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  4. Codeforces Round #316 (Div. 2)

    A. Elections time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  5. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  6. Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树

    C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...

  7. 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 ...

  8. Codeforces Round #353 (Div. 2) D. Tree Construction 模拟

    D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...

  9. 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 ...

随机推荐

  1. Lightoj1084【DP啊DP】

    题意: 给你n个人的位置,每个人最多移动k个单位,然后在某点>=3人可以抱团,问你这n个人最少抱团数,只要有一个n不能抱团输出-1: 思路: 感觉又是超级超级狗血.... 剪不断,理还乱... ...

  2. Codevs 1293 送给圣诞夜的极光

    1293 送给圣诞夜的极光  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 圣诞老人回到了北极圣 ...

  3. 第四章 “我要点爆”微信小程序云开发之疯狂点击与糖果点爆页面制作

    疯狂点击点爆方式页面制作 疯狂点击为用户提供一个60秒的按钮点击时间,同时点击过程中有背景音乐,系统根据用户点击按钮的此时来进行热度值的计算. <view class="the_hea ...

  4. activestate.com网站导航条

  5. iOS开发 - 多线程实现方案之NSOperation篇

    NSOperation简介 1.实现多线程编程步骤: 配合使用NSOperation和NSOperationQueue实现多线程编程,我们不用考虑线程的生命周期.同步.加锁等问题,如下: 先将需要执行 ...

  6. Windows类标识符及其妙用

    Windows类标识符 百度百科这样解释: Windows的类标识符class identifier也称为CLASSID或CLSID,是与某一个类对象相联系的唯一标记(UUID).一个准备创建多个对象 ...

  7. VC 中TEXT、_T、L的区别

    http://i.cnblogs.com/EditPosts.aspx?opt=1 对于从VC++6.0转到VS2005编译环境中的程序员.往往会碰到字符集之间的转换. VC6.0采用的是ANSI字符 ...

  8. PAT甲级——1134 Vertex Cover (25 分)

    1134 Vertex Cover (考察散列查找,比较水~) 我先在CSDN上发布的该文章,排版稍好https://blog.csdn.net/weixin_44385565/article/det ...

  9. java算法之冒泡排序

    所有的冒泡排序算法都是一样的: 冒泡排序可以分为两种情况: 一种是将无序序列排序为由小到大的升序方式: 另一种情况是将无序序列转化成一种由大到小的降序排列:

  10. Excel 通过pl/sql导入到数据库 文本导入器 odbc导入器

     Excel 通过pl/sql导入到数据库 第一种方法:文本导入器 1.准备Excel导入数据   jc.xls 2.把 jc.xls 文件 改为 jc.csv文件 3.在数据库里建一张jc表(FLH ...