Codeforces Round #316 (Div. 2) D DFS+vector+二分

来源:互联网 发布:华东师范大学知乎 编辑:程序博客网 时间:2024/06/14 06:25



链接:戳这里


D. Tree Requests
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".


题意:

n个节点一棵树,每个节点对应一个小写字母

q个询问vi,hi 表示深度为hi的节点并且vi是这些节点的祖先,能否任意组他们的小写字母变成回文串


思路:

算出每个节点的深度deep[i],以及dfs序(每个节点对应的左右区间)

这里有一个O(1)判断是否是回文串的技巧,每个节点的字母都一一对应a[i](1<<(小写字母-'a')),统计一个字母出现的次数,当奇数个字母超过两次的话就无法组成回文串,每次询问统计同深度同祖先的节点的所有a[i]的抑或和tmp,这样只要tmp二进制位上出现超过两个1就不是回文串

如果快速找出同深度同祖先的所有节点的抑或和?

vector存下每个深度对应的节点dfs序的左区间l[i],以及当前同深度的所有区间的抑或和

每次对于给出的vi,hi 。只需要找出vector[hi]下所有的满足是以vi为祖先的节点

由于每个节点的dfs序[l,r]是已知的也是唯一的。那么二分这个区间并快速找出符合要求的区间的抑或和

具体看代码


代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1using namespace std;struct edge{    int v,next;}e[1000100];int head[500100],tot=0;int n,m;void Add(int u,int v){    e[tot].v=v;    e[tot].next=head[u];    head[u]=tot++;}int l[500100],r[500100],deep[500100],a[500100];int cnt=0;vector<pair<int,int> > V[500100];string s;void DFS(int u,int fa,int d){    l[u]=++cnt;    deep[u]=d;    V[d].push_back(make_pair(cnt,V[d].back().second^(1<<a[u])));    for(int i=head[u];i!=-1;i=e[i].next){        int v=e[i].v;        if(v==fa) continue;        DFS(v,u,d+1);    }    r[u]=++cnt;}int num(int x){    int ans=0;    while(x&-x){        ans++;        x-=x&-x;    }    return ans;}int main(){    mst(head,-1);    scanf("%d%d",&n,&m);    for(int i=2;i<=n;i++){        int x;        scanf("%d",&x);        Add(x,i);        Add(i,x);    }    cin>>s;    for(int i=0;i<s.size();i++) a[i+1]=s[i]-'a';    for(int i=1;i<=n;i++) V[i].push_back(make_pair(0,0)); /// 方便计算抑或和    DFS(1,0,1);    for(int i=1;i<=m;i++){        int x,h;        scanf("%d%d",&x,&h);        if(deep[x]>=h){            printf("Yes\n");            continue;        }        int L=lower_bound(V[h].begin(),V[h].end(),make_pair(l[x],-1))-V[h].begin();/// 找出V[h]里的第一个大于祖先的左区间的下标        int R=lower_bound(V[h].begin(),V[h].end(),make_pair(r[x],-1))-V[h].begin();/// 找出v[h]里的第一个大于祖先的右区间的下标        L--;R--;        /// 这里-1类似前缀和的过程    ///    cout<<L<<" "<<R<<endl;        if(num(V[h][R].second^V[h][L].second)<=1) printf("Yes\n");        else printf("No\n");    }    return 0;}


0 0
原创粉丝点击