Tree Requests (dfs序,二进制优化,好题)

来源:互联网 发布:大数据调研提纲 编辑:程序博客网 时间:2024/06/01 08:54

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 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 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 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 51 1 1 3 3zacccd1 13 34 16 11 2
output
YesNoYesYesYes
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".


原题链接


题意:给你一棵有父子关系的树,每个节点有一个字母,m次询问,问在h深度且为v节点的孩子们的那些节点能不能组成回文串(根的深度为1)。

我们可以用dfs序来判断节点是否为v的子节点,用一个vector二维记录每个深度的节点到达的时间(便于二分),并记录每个时间戳是哪个节点。然后询问时就可以先确定深度,然后在深度里找到是v的子节点的那些点(用二分),并判断是否可以组成回文。

mark:lower_bound是找第一个大于等于询问值的点,upper_bound是找第一个严格大于询问值的点。

dfs序,标记进入u的时间戳和离开u的时间戳。

回文即最多有一个字母是奇数个。

用二进制标记每个字母出现奇偶次,这样对每个深度的所有字母预处理后复杂度大大降低。


#include<cstdio>#include<iostream>#include<algorithm>#include<queue>#include<stack>#include<cstring>#include<string>#include<vector>#include<cmath>#include<map>using namespace std;typedef long long ll;#define mem(a,b) memset(a,b,sizeof(a))const int maxn = 5e5+5;const int ff = 0x3f3f3f3f;const double esp = 1e-7;int n,m,t,depth;vector<int> mp[maxn];vector<int> deep[maxn];vector<int> xoor[maxn];int l[maxn],r[maxn],w[maxn];char c[maxn];void dfs(int x,int d){l[x] = ++t;depth = max(depth,d);deep[d].push_back(l[x]);//压入到达的时间w[l[x]] = x;//记录这个时间是哪个点int k = mp[x].size();for(int i = 0;i< k;i++)dfs(mp[x][i],d+1);r[x] = t;//离开的时间戳等于进入下一个点的时间戳}int solve(int lb,int rb,int h){int ans = 0,det;if(rb< 0)return 0;if(lb-1>= 0)//计算出lb~rb之间的字母个数值 det = xoor[h][lb-1]^xoor[h][rb]; elsedet = xoor[h][rb];while(det)//计算有多少个个数为奇数的字母 {ans+= det&1;det>>= 1;}return ans;}int main(){cin>>n>>m;for(int i = 2;i<= n;i++){int t;scanf("%d",&t);mp[t].push_back(i);}for(int i = 1;i<= n;i++)scanf(" %c",&c[i]);dfs(1,1);for(int i = 1;i<= depth;i++)//预处理每个深度的字母出现的次数{xoor[i].push_back(1<<(c[w[deep[i][0]]] - 'a'));int k = deep[i].size();for(int j = 1;j< k;j++)xoor[i].push_back((1<<(c[w[deep[i][j]]] - 'a'))^xoor[i][j-1]);}int v,h;while(m--){scanf("%d %d",&v,&h);int lb = lower_bound(deep[h].begin(),deep[h].end(),l[v])-deep[h].begin();//第一个大于等于进入时间戳的点int rb = upper_bound(deep[h].begin(),deep[h].end(),r[v])-deep[h].begin()-1;//第一个大于离开时间戳的点-1if(solve(lb,rb,h)> 1)printf("No\n");elseprintf("Yes\n");}return 0;}


阅读全文
0 0