链接:http://codeforces.com/problemset/problem/570/D

D. Tree Requests
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Roman planted a tree consisting of n vertices. Each vertex contains a lowercase English letter. Vertex 1 is
the root of the tree, each of the n - 1 remaining vertices has a parent in
the tree. Vertex is connected with its parent by an edge. The parent of vertex i is vertex pi,
the parent index is always less than the index of the vertex (i.e., pi < i).

The depth of the vertex is the number of nodes on the path from the root to v along
the edges. In particular, the depth of the root is equal to 1.

We say that vertex u is in the subtree of vertex v,
if we can get from u to v,
moving from the vertex to the parent. In particular, vertex v is in its subtree.

Roma gives you m queries, the i-th
of which consists of two numbers vihi.
Let's consider the vertices in the subtree vi located
at depthhi.
Determine whether you can use the letters written at these vertices to make a string that is a palindrome. The letters that are written in the vertexes, can be rearranged in any order to make
a palindrome, but all letters should be used.

Input

The first line contains two integers nm (1 ≤ n, m ≤ 500 000)
— the number of nodes in the tree and queries, respectively.

The following line contains n - 1 integers p2, p3, ..., pn —
the parents of vertices from the second to the n-th (1 ≤ pi < i).

The next line contains n lowercase English letters, the i-th
of these letters is written on vertex i.

Next m lines describe the queries, the i-th
line contains two numbers vihi (1 ≤ vi, hi ≤ n)
— the vertex and the depth that appear in thei-th query.

Output

Print m lines. In the i-th
line print "Yes" (without the quotes), if in the i-th
query you can make a palindrome from the letters written on the vertices, otherwise print "No" (without the quotes).

Sample test(s)
input
6 5
1 1 1 3 3
zacccd
1 1
3 3
4 1
6 1
1 2
output
Yes
No
Yes
Yes
Yes
Note

String s is a palindrome if reads the same from left to right and from
right to left. In particular, an empty string is a palindrome.

Clarification for the sample test.

In the first query there exists only a vertex 1 satisfying all the conditions, we can form a palindrome "z".

In the second query vertices 5 and 6 satisfy condititions, they contain letters "с" and "d"
respectively. It is impossible to form a palindrome of them.

In the third query there exist no vertices at depth 1 and in subtree of 4. We may form an empty palindrome.

In the fourth query there exist no vertices in subtree of 6 at depth 1. We may form an empty palindrome.

In the fifth query there vertices 2, 3 and 4 satisfying all conditions above, they contain letters "a", "c"
and "c". We may form a palindrome "cac".

题意:

告诉你一颗树的父子关系,1节点为根。再告诉你每一个点上的字母。

问 v节点 子树(包含v节点)在第h行的全部节点的字母是否能组成回文串。

做法:

先用dfs 搜索 把全部节点标个左标号和右标号。 这样标号以后。每一个节点 用左标号 当自己 新的标号。 然后  子树全部节点 的新标号 肯定在 子树根节点的 左右标号之间。

标号之后分层来做。

每层  对每一个字母分别做统计。

把该层全部节点 的 左标号 在树状数组中+1. 然后对于该层的全部询问 做 树状数组统计。(sum(rit[v])-sum(lft[v]-1))。

假设是奇数 说明这个 字母在查询的区间内 有奇数个。

每一个查询  最多有一个奇数个的字母。否则不能构成回文串

#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<math.h> using namespace std;
const int N = 500100; int f[N];
vector<int> son[N];
int id;
int lft[N],rit[N];
int deps[N];
int ans[N];
char str[N];
vector<int> G[N];//深度
vector<pair<int,int> > Q[N];
void dfs(int nw,int dep)
{
lft[nw]=id++;
deps[nw]=dep;
G[dep].push_back(nw);
for(int i=0;i<son[nw].size();i++)
{
int to=son[nw][i];
dfs(to,dep+1);
}
rit[nw]=id++;
} int bit[2*N]; int lowbit(int x)
{
return x&(-x);
} void add(int wei,int x)
{ while(wei<=id)
{
bit[wei]+=x;
wei+=lowbit(wei);
}
} int sum(int wei)
{
if(wei==0)
return 0;
int sum=0;
while(wei>0)
{
sum+=bit[wei];
wei-=lowbit(wei);
}
return sum;
} int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=2;i<=n;i++)
{
scanf("%d",f+i);
son[f[i]].push_back(i);
}
scanf("%s",str+1);
id=1;
dfs(1,1);
int dep=1;
for(int i=1;i<=m;i++)
{
int vv,hh;
scanf("%d%d",&vv,&hh);
dep=max(hh,dep);
Q[hh].push_back(make_pair<int,int>(vv,i));
} for(int i=1;i<=dep;i++)
{
for(int j=0;j<26;j++)
{
if(j==25)
int kkk=1;
for(int k=0;k<G[i].size();k++)//每一个节点
{
if(str[G[i][k]]-'a'==j)
add(lft[G[i][k]],1);
} for(int k=0;k<Q[i].size();k++)
{
int v=Q[i][k].first;
int ii=Q[i][k].second;
if((sum(rit[v])-sum(lft[v]-1))&1) ans[ii]++;
} for(int k=0;k<G[i].size();k++)//每一个节点
{
if(str[G[i][k]]-'a'==j)
add(lft[G[i][k]],-1);
}
// printf("jj %d \n",j);
} //printf("ii %d \n",i);
} for(int i=1;i<=m;i++)
{
if(ans[i]<=1)
printf("Yes\n");
else
printf("No\n");
} }
return 0;
}

codeforces 570 D. Tree Requests 树状数组+dfs搜索序的更多相关文章

  1. HDU5293(SummerTrainingDay13-B Tree DP + 树状数组 + dfs序)

    Tree chain problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  2. Ultra-QuickSort(树状数组求逆序对数)

    Ultra-QuickSort 题目链接:http://poj.org/problem?id=2299 Time Limit: 7000MS   Memory Limit: 65536K Total ...

  3. hdu2838树状数组解逆序

    离散化和排序后的序号问题搞得我实在是头痛 不过树状数组解逆序和偏序一类问题真的好用 更新:hdu的数据弱的真实,我交上去错的代价也对了.. 下面的代码是错的 /* 每个点的贡献度=权值*在这个点之前的 ...

  4. 【BZOJ】2434: [Noi2011]阿狸的打字机 AC自动机+树状数组+DFS序

    [题意]阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的: l 输入小写 ...

  5. Codeforces 1076E Vasya and a Tree(树状数组)或dfs

    题意:给你一颗以1为根节点的树,初始所有节点的权值为0,然后有m个操作,每个操作将点x的所有距离不超过d的节点权值+1,问经过m次操作后每个节点权值是多少? 思路:如果是一个序列,就可以直接用树状数组 ...

  6. codeforces 570 D. Tree Requests (dfs)

    题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...

  7. codeforces#1167F. Scalar Queries(树状数组+求贡献)

    题目链接: https://codeforces.com/contest/1167/problem/F 题意: 给出长度为$n$的数组,初始每个元素为$a_i$ 定义:$f(l, r)$为,重排$l$ ...

  8. poj 3321:Apple Tree(树状数组,提高题)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Descr ...

  9. POJ3321 Apple Tree (树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16180   Accepted: 4836 Descr ...

随机推荐

  1. PHP下mysql驱动概述

    Overview of the MySQL PHP drivers 什么是API? 一 个应用程序接口(Application Programming Interface的缩写),定义了类,方法,函数 ...

  2. Python之实时调度任务

    过了年就开始面临毕业了,毕业季是幸福的,但也是紧迫的.2月已过就开始着手策划毕业论文了,一直到现在,论文基本完成了,感觉过去的一个半月的时间,对自己过去2年在实验室所学又温习了一遍.书到用时方恨少,在 ...

  3. 洛谷P2664 树上游戏 【点分治 + 差分】

    题目 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 现在他想让你求出所有的sum[i] 输入格式 第一行为一个整数n,表示树节点的数量 ...

  4. hdu5852 Intersection is not allowed! 【矩阵行列式】

    题意 给出\(n*n\)网格\((n<=10^5)\) 顶部有\(K\)个起点,底部有\(K\)个相对应的终点 每次只能向下或向右走 求有多少种从各个起点出发到达对应终点且路径不相交的路径? 对 ...

  5. 单线程实现并发——协程,gevent模块

    一 并发的本质 1 切换 2 保存状态 二 协程的概念 协程,又称微线程,纤程.英文名Coroutine.单线程下实现并发,用户从应用程序级别控制单线程下任务的切换,注意一定是遇到I/O才切. 协程的 ...

  6. Linux System Programming 学习笔记(九) 内存管理

    1. 进程地址空间 Linux中,进程并不是直接操作物理内存地址,而是每个进程关联一个虚拟地址空间 内存页是memory management unit (MMU) 可以管理的最小地址单元 机器的体系 ...

  7. Android上下文Context

    Android上下文Context介绍 在应用开发中最熟悉而陌生的朋友-----Context类 ,说它熟悉,是应为我们在开发中时刻的在与它打交道,例如:Service.BroadcastReceiv ...

  8. 二、 java中的变量与数据类型及类型转换

    标识符:凡是可以自己命名的地方都叫标识符,如:类名.方法名.接口名... 1.标识符命名的规则: 由26个英文字母大小写,0-9,_或$组成,不遵守会报错. 不可以用数字开头. 不能使用关键字和保留字 ...

  9. ARCGIS SDK For DotNet 路径

    ARCGIS SDK For DotNet 路径 驱动器 C 中的卷是 WIN7 卷的序列号是 06AC-BD3E C:\Program Files (x86)\ArcGIS\DeveloperKit ...

  10. What is pseudopolynomial time? How does it differ from polynomial time?

    To understand the difference between polynomial time and pseudopolynomial time, we need to start off ...