【Codeforces 570D 】Tree Requests dfs序+二分

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

Sample test(s)
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个点的树,m个询问 in[maxn],out[maxn];
每个点有一个字母
询问形如 (u, deep) 问u点的子树中,距离根的深度为deep的所有点的字母能否在任意排列后组成回文串,能输出Yes.

思路:dfs序,以时间撮建vector sta[maxn][27];

把距离根深度相同的点都存到vector里 D[i] 表示深度为i的所有点,在dfs时可以顺便求出。

把询问按深度排序,query[i]表示所有深度为i的询问。

接下来按照深度一层层处理。

对于第i层,把所有处于第i层的节点都更新到26个树状数组上。

然后处理询问,直接查询树状数组上有多少种字母是奇数个的,显然奇数个字母的种数要<=1

处理完第i层,就把树状数组逆向操作,相当于清空树状数组

注意的一个地方就是 询问的深度是任意的,也就是说可能超过实际树的深度,也可能比当前点的深度小。。所以需要初始化一下答案。。

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#define maxn 500005
using namespace std;
vector<int> sta[maxn][27];
int in[maxn],out[maxn];
int time,n,m;
vector<int> lin[maxn];
int val[maxn];
void dfs(int x,int id)
{
    in[x]=++time;
    sta[id][val[x]].push_back(time);
    for(int i=0;i<lin[x].size();i++)  
        dfs(lin[x][i],id+1);
    out[x]=time;
}
//bool solve(int x,int dep)
//{
// int sum=0;
// for(int i=0;i<26;i++)
// {
// int num=(upper_bound(sta[dep][i].begin(),sta[dep][i].end(),out[x])-lower_bound(sta[dep][i].begin(),sta[dep][i].end(),in[x]));
// if(num%2)
// {
// sum++;
// if(sum>1) return false;
// }
// }
// return true;
//}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=2;i<=n;i++)
    {
        int aa;
        scanf("%d",&aa);
        lin[aa].push_back(i);
    }
    for(int i=1;i<=n;i++)
    {
        char c;
        scanf(" %c",&c);    
        val[i]=c-'a';
    }   
    dfs(1,1);
    while(m--)
    {
        int x;int dep;
        scanf("%d%d",&x,&dep);
        int sum=0;
        int inn=in[x];
        int ou=out[x];
        for(int i=0;i<26;i++)
        {
            int num=(upper_bound(sta[dep][i].begin(),sta[dep][i].end(),ou)-lower_bound(sta[dep][i].begin(),sta[dep][i].end(),inn));
            if(num%2)
            {
                sum++;
                if(sum>1) break;;
            }
        }
        if(sum>1) printf("No\n");
        else
        printf("Yes\n");
    }
}

位运算优化后

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#define maxn 500005
using namespace std;
vector<int> sta[maxn];
int in[maxn],out[maxn];
int time,n,m;
int ma;
vector<int>f[maxn];
vector<int> lin[maxn];
int hash[maxn];
int val[maxn];
void dfs(int x,int id)
{
    ma=max(ma,id);
    in[x]=++time;
    hash[time]=val[x];
    f[id].push_back(time);
    for(int i=0;i<lin[x].size();i++)  
        dfs(lin[x][i],id+1);
    out[x]=time;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=2;i<=n;i++)
    {
        int aa;
        scanf("%d",&aa);
        lin[aa].push_back(i);
    }
    for(int i=1;i<=n;i++)
    {
        char c;
        scanf(" %c",&c);    
        val[i]=c-'a';
    }   
    dfs(1,1);
    for(int i=1;i<=ma;i++)  
    {  
        sta[i].push_back(1<<(hash[f[i][0]]));  
        for(int j=1;j<f[i].size();j++)  
        {  
            sta[i].push_back((1<<(hash[f[i][j]]))^sta[i][j-1]);  
        }  
    }  
    while(m--)
    {
        int x;int dep;
        scanf("%d%d",&x,&dep);
        int sum=0;
        int l =lower_bound(f[dep].begin(),f[dep].end(),in[x])-f[dep].begin();  
        int r =upper_bound(f[dep].begin(),f[dep].end(),out[x])-f[dep].begin();

        l--;
        r--;  
        if(r<0)     printf("Yes\n");
        else
        {
                int tt=sta[dep][r];
        if(l>=0)
        tt^=sta[dep][l];
        sum=(__builtin_popcount(tt)); 
        if(sum>1) printf("No\n");
        else
        printf("Yes\n");
        }

    }
}

你可能感兴趣的:(DFS,codeforces)