time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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 vi, hi. Let’s consider the vertices in the subtree vi located at depth hi. 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 n, m (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 vi, hi (1 ≤ vi, hi ≤ n) — the vertex and the depth that appear in the i-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).

Examples

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

【题目链接】:http://codeforces.com/contest/570/problem/D

【题解】



题意:

给你m个询问,每个询问让你在以vi为根节点的子树里面,找到深度为hi的所有节点,这些节点的节点上都有一个字符;问你这些字符(只有小写字母,可以交换顺序)能不能组成一个回文串;

做法:

利用异或的性质;

用zt[i]表示深度为i时,a-z这些字母出现次数的奇偶性(0表示偶数,1表示奇数,可以用0..2^26的二进制来表示->奇偶性的改变对应异或);

(回文串的话,显然最后二进制的zt[i]里面只能有一个1或没有1);

我用程序说

void dfs(int x,int d)
{
int len = q[x].size();
/*
在进入一个节点前;
先“知道”在没有进入它的子树前
与他有关的询问的深度为h[i]的所有节点的字符状态;
在出来的时候再异或一次,就能知道在我们遍历它的子树时,那些深度为h[i]的节点的字符状态发生了哪些变化,如果没有变化的话,最后结果就为0->表示这个子树里面没有满足要求的深度为h[i]的节点;则为空串,那样也符合题意;
*/
rep1(i,0,len-1)
{
int id = q[x][i].fi,dd=q[x][i].se;
ans[id] ^= zt[dd];
}
len = a[x].size();
rep1(i,0,len-1)
{
int y = a[x][i];
dfs(y,d+1);
}
zt[d]^=1<<(s[x]-'a');//更改深度为d的节点所拥有的字母的奇偶性
len = q[x].size();
rep1(i,0,len-1)//再异或一次,就能得到这一次遍历子树增加了哪些新的字母了,且可得它们的奇偶性.
{
int id = q[x][i].fi,dd=q[x][i].se;
ans[id] ^= zt[dd];
}
}

【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second typedef pair<int,int> pii;
typedef pair<LL,LL> pll; void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} const int MAXN = 5e5+100;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0); int n,m;
vector <int> a[MAXN];
vector <pii> q[MAXN];
int zt[MAXN] = {0},ans[MAXN];
char s[MAXN]; void dfs(int x,int d)
{
int len = q[x].size();
rep1(i,0,len-1)
{
int id = q[x][i].fi,dd=q[x][i].se;
ans[id] ^= zt[dd];
}
len = a[x].size();
rep1(i,0,len-1)
{
int y = a[x][i];
dfs(y,d+1);
}
zt[d]^=1<<(s[x]-'a');
len = q[x].size();
rep1(i,0,len-1)
{
int id = q[x][i].fi,dd=q[x][i].se;
ans[id] ^= zt[dd];
}
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);rei(m);
rep1(i,2,n)
{
int p;
rei(p);
a[p].pb(i);
}
scanf("%s",s+1);
rep1(i,1,m)
{
int vi,hi;
rei(vi);rei(hi);
q[vi].pb(mp(i,hi));
}
dfs(1,1);
rep1(i,1,m)
{
if (ans[i]&(ans[i]-1))// 00100-1=00011;00100&00011==0;
puts("No");
else
puts("Yes");
}
return 0;
}

【19.77%】【codeforces 570D】Tree Requests的更多相关文章

  1. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  2. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  3. codeforces 570 D. Tree Requests (dfs)

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

  4. codeforces 570 D Tree Requests

    题意:给出一棵树.每一个结点都有一个字母,有非常多次询问,每次询问.以结点v为根的子树中高度为h的后代是否可以经过调整变成一个回文串. 做法: 推断能否够构成一个回文串的话,仅仅须要知道是否有大于一个 ...

  5. 【77.78%】【codeforces 625C】K-special Tables

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  6. 【19.46%】【codeforces 551B】ZgukistringZ

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【13.77%】【codeforces 734C】Anton and Making Potions

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【77.39%】【codeforces 734A】Anton and Danik

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【44.19%】【codeforces 608D】Zuma

    time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

随机推荐

  1. 计算加班类型以及小时数(js)

    function GetDateDiff(startTime, endTime, diffType) { //将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式 startTime ...

  2. wget---从指定的URL下载文件

    wget命令用来从指定的URL下载文件.wget非常稳定,它在带宽很窄的情况下和不稳定网络中有很强的适应性,如果是由于网络的原因下载失败,wget会不断的尝试,直到整个文件下载完毕.如果是服务器打断下 ...

  3. 03011_预处理对象executeUpdate方法(实现数据库的增、删、改)

    1.概述 (1)通过预处理对象的executeUpdate方法,完成记录的insert\update\delete语句的执行: (2)操作格式统一如下: ①注册驱动: ②获取连接: ③获取预处理对象: ...

  4. 洛谷——P1311 选择客栈

    https://www.luogu.org/problem/show?pid=1311 题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰 ...

  5. [Python] Python's namedtuples can be a great alternative to defining a class manually

    # Why Python is Great: Namedtuples # Using namedtuple is way shorter than # defining a class manuall ...

  6. gridView -item 大小调节(dimen-代码引用)

    今天在修改一个gridview的时候,发现里面的内容并不会自动适应,填满整个gridview,而是会产生滑动,尝试了很多的方法,包括在item文件中设定width和height,结果,宽度可调,高度却 ...

  7. golang sync.Mutex

    //go func 和主线程之间的关系是并行和竞争关系 package main import ( "fmt" "sync" "time" ...

  8. Codefroces 760 B. Frodo and pillows

    B. Frodo and pillows time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. Appium_pytest fixture的使用

    一.定义fixture方法 # -*- coding:utf-8 -*-import pytestfrom baseutil.DriverUtil import DriverConfig @pytes ...

  10. vue.js提交按钮时简单的if判断表达式示例

    简单的业务需求如下,看图说话 1:当充值金额没有填写的时候,会有Toast小弹框提示"请输入有效的充值金额" if (!this.money) { console.log('mon ...