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;
}

最新文章

  1. PHP的魔法方法__set() __get()
  2. Spark实战3:Maven_Java_HelloWorld
  3. linux 查看服务器性能常用命令
  4. 一种少见的跨目录写webshell方法
  5. 追加文件内容java
  6. Keepalived原理及配置应用总结
  7. URAL 1784 K - Rounders 找规律
  8. 水平/竖直居中在旧版Safari上的bug
  9. 关于用PS改变图像颜色
  10. css常用属性1
  11. PHP算法学习(8) 环形链表 解决约瑟夫问题
  12. JavaScript如何工作:内存管理+如何处理4个常见的内存泄漏
  13. 学习html5的网站
  14. Visual studio2015 编译时提示“GenerateResource”任务意外失败。
  15. Oracle varchar与varchar2的区别
  16. android -------- Android Studio调试运行时ADB not responding
  17. Java多线程的同步控制记录
  18. nginx通过配置empty_gif解决请求favicon 404的问题
  19. linux系统下php扩展的安装
  20. hdu1800Flying to the Mars (字典树)

热门文章

  1. element-ui一些注意点:
  2. 【DRF序列化】
  3. NSCharacterSet 最经常使用的使用方法
  4. smarty模板引擎(一)基础知识
  5. php excel文件导出之phpExcel扩展库
  6. var和ES6的let
  7. vue移动端上拉加载更多
  8. ProgressBar-style属性分析
  9. Android学习笔记之Bitmap位图的缩放
  10. 2.Web开发过程流程图