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

CrossDolphin發表於2016-07-14



連結:戳這裡


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)-1
using 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;
}


相關文章