F

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).
#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];
    }
    else
    {
        det = 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;//第一个大于离开时间戳的点-1
        if(solve(lb,rb,h)> 1)
        {
            printf("No\n");
        }
        else
        {
            printf("Yes\n");
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值