官方题解是离线询问,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. Eclipse 安装Maven插件

    这个好: http://www.iteye.com/topic/1123225 其他: 1先安装subeclipse插件就是svn svn - http://subclipse.tigris.org/ ...

  2. 利用StringBuffer来替换内容

    package com.test.java; public class StringBufferTest { public static void main(String[] args) { Stri ...

  3. Codeforces712B【= =】

    题意: 题意: 最少需要几步改变能够使得按照原序列的走法从起源走到起源: 思路: 长度奇数肯定不行 偶数的情况下.. 其实题意转变就是有4个数a,b,c,d,在最小的改变下,使a==b,c==d; 那 ...

  4. grunt和seajs入门之--提取依赖、合并、压缩js文件

    一.安装grunt: npm install -g grunt-cli //安装 npm install grunt –save-dev //安装Grunt最新版本到项目目录中,并将其添加到devDe ...

  5. VC.NET 需要注意的一些问题

    String^ abc = gcnew String(); String^ abc = nullptr override .net 类方法: void Reset() override {....} ...

  6. IT兄弟连 JavaWeb教程 EL与JSTL表达式经典面试题

    1.简述EL表达式的作用 EL表达式的作用可分为以下三类 访问Bean的属性. 输出简单的运算结果. 获取请求参数值. 2.JSP标签的作用?如何定义? JSP标签可以分离JSP页面的内容和逻辑,业务 ...

  7. linux 搭建unixODBC ,并对接 PostgreSQL 9.3.4

    环境:suse 11 ,64位的操作系统 unixODBC 版本:2.3.2 PostgreSQL 9.3.4 1 编译安装 unixODBC 下载 unixODBC :http://www.unix ...

  8. 理解:return、break、continue区别

    1.return:结束该方法的执行 2.continue:结束当前流程中的continue一下的代码,如果是for循环会再次执行下一个条件 3.break:完全终止当前执行流程,如果是for循环,那么 ...

  9. HTML--使用重置按钮,重置表单信息

    当用户需要重置表单信息到初始时的状态时,比如用户输入“用户名”后,发现书写有误,可以使用重置按钮使输入框恢复到初始状态.只需要把type设置为"reset"就可以. 语法: < ...

  10. CATIA 基础详解 第01章 CATIA初认识

    1.1 CATIA V5产品介绍 CATIA V5是基于美国IBM公司与法国达索系统公司(Dassault Systèmes)软件解决方案推出的新一代产品,它致力于满足以设计流程为中心的设计需求.它提 ...