Codeforces 570D Tree Requests【Dfs序+二分】好题!

本文介绍了一道关于树形结构数据与回文串判定的算法题,通过深度优先搜索(DFS)预处理和二分查找技术解决特定查询问题。文章详细解释了如何利用DFS序来快速定位子树中的节点,并提出了一个高效的AC代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. 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 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个点,有M个查询,告诉你每个点的父亲节点的编号。

然后给你一个长度为N的字符串,表示每个点所代表的字符。

让你判断以x为根的子树距离根(1)的深度为y的所有字符是否能够构成一个回文串。


思路(思路来源于网络):


1、判定一堆字符能否构成回文串的方法:对于26个字符中,出现奇数次的字符个数要<=1.


2、我们已知可以O(n)预处理出Dfs序,设定L【u】表示Dfs过程中,进入节点u的时间戳为L【u】.设定R【u】表示Dfs过程中,离开节点u的时间戳为R【u】;

那么如果有点v.其L【u】<=L【v】<=R【u】.那么v这个点就是u的子节点。

那么我们维护一个三维数组vector:Deep【i】【26(j)】,用于存入L【u】:满足u节点深度为i,代表的字符为j


3、那么我们对应一个查询x,y.我们枚举每一种字符,对应查询Deep【y】【i(枚举的字符)】中,大于等于L【x】并且小于等于R【x】的元素的个数。

这里二分实现即可。

注意一些细节,这题卡了常,能用位运算的部分尽量用位运算。

能优化的常数尽量去优化。


Ac代码(1900+ms Ac):

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
char str[500500];
int L[500500];
int R[500500];
vector<int >mp[500500];
vector<int >Deep[500500][28];
int cnt;
void Dfs(int u,int from,int depp)
{
    L[u]=++cnt;
    Deep[depp][str[u]-'a'+1].push_back(L[u]);
    for(int i=0; i<mp[u].size(); i++)
    {
        int v=mp[u][i];
        Dfs(v,u,depp+1);
    }
    R[u]=cnt;
}
int FindL(int x,int y,int j)
{
    int ans=-1;
    int l=0;
    int r=Deep[y][j].size()-1;
    while(r-l>=0)
    {
        int mid=(l+r)/2;
        if(Deep[y][j][mid]>=L[x])
        {
            ans=mid;
            r=mid-1;
        }
        else l=mid+1;
    }
    return ans;
}
int FindR(int x,int y,int j,int LLL)
{
    int ans2=-1;
    int l=0;
    int r=Deep[y][j].size()-1;
    while(r-l>=0)
    {
        int mid=(l+r)/2;
        if(Deep[y][j][mid]<=R[x])
        {
            ans2=mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    return ans2;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        cnt=0;
        for(int i=2; i<=n; i++)
        {
            int u;
            scanf("%d",&u);
            mp[u].push_back(i);
        }
        scanf("%s",str+1);
        Dfs(1,-1,1);
        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            int flag=0;
            int ttt=0;
            for(int j=1; j<=26; j++)
            {
                int ans=FindL(x,y,j);
                if(ans==-1)continue;
                int ans2=FindR(x,y,j,ans);
                int num=(ans2-ans+1);
                if(num&1)
                {
                    ttt++;
                    if(ttt>1)break;
                }
            }
            if(ttt>1)printf("No\n");
            else printf("Yes\n");
        }
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值