cf570D. Tree Requests(dsu on tree)

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

分析:
感觉cf上的题,题意都很清奇
这道题的大意是:
给你一个树形结构,每个节点上有一个字母
询问(x,y)
即在整棵树中深度为y,且位于x的子树中的节点
能不能构成一个回文串(空串也算是回文串)

首先分析一下回文串的性质:
串中个数为奇数的字母<=1

那我们就可以把问题转化成判定子树中一些特定节点的不同字母数

写完这道题
感觉才对dsu on tree有了初步了解
曲神说这是一种很好的解决树上问题的思想,很值得学习
主要操作就只有三个:找重儿子,dfs,记录儿子的影响

以这道题为例吧
因为这道题涉及到了深度,所以我们在找重儿子的时候要记录一下每个节点的深度

dfs流程:
1.dfs轻儿子
2.dfs重儿子,标记重儿子
3.把当前节点的信息统计到ta的父亲上
4.重儿子标记清空
5.记录答案/处理询问
6.一键清除轻儿子影响
大同小异。。。

在传递儿子的信息时
我们只考虑这一个节点的信息,并进行统计
那节点所代表的子树呢,通过dfs解决(注意有!=Son的限制)

tip

变量名不要搞混了
一开始我在读入字符串的时候,实用的%c一个一个读入的,
不停RE
之后改成了%s,就A了
这给我们一个启示,读字符串的时候用%s就好,不要拆开读
如果想让字符从1开始存储,
scanf(“%s”,c+1);

60+个点,怀疑人生

这里写代码片
#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

const int N=500010;
struct node{
    int x,y,nxt,bh;
};
node way[N],q[N];
int son[N],size[N],Son,n,m,tot=0,st[N];
int tt=0,stq[N],deep[N],cnt[N][30];
bool ans[N];
char c[N];

void add(int u,int w)
{
    tot++;
    way[tot].x=u;way[tot].y=w;way[tot].nxt=st[u];st[u]=tot;
}

void addq(int u,int w,int b)   //把询问也连起来 
{
    tt++;
    q[tt].x=u;q[tt].y=w;q[tt].bh=b;q[tt].nxt=stq[u];stq[u]=tt;
}

void getson(int x,int dep)
{
    int i,mx=0;
    size[x]=1;
    deep[x]=dep;
    for (i=st[x];i;i=way[i].nxt)
    {
        getson(way[i].y,dep+1);
        size[x]+=size[way[i].y];
        if (size[way[i].y]>mx)
        {
            mx=size[way[i].y];
            son[x]=way[i].y;
        }
    }
}

void js(int x,int vv)
{
    cnt[deep[x]][c[x]-'a']+=vv;  //单个节点记录信息 
    for (int i=st[x];i;i=way[i].nxt)
        if (way[i].y!=Son)
           js(way[i].y,vv);
}

void dfs(int x,int keep)
{
    int i,j;
    for (i=st[x];i;i=way[i].nxt)  
        if (way[i].y!=son[x])
            dfs(way[i].y,0);
    if (son[x]) dfs(son[x],1),Son=son[x];   
    js(x,1);
    Son=0;
    for (i=stq[x];i;i=q[i].nxt)  //处理询问 
    {
        int v=q[i].y,o=0;
        for (j=0;j<26;j++)
            if (cnt[v][j]&1) o++;  //深度为v 
        if (o<=1) ans[q[i].bh]=1;
        else ans[q[i].bh]=0;
    }
    if (!keep) js(x,-1);
}

int main()
{
    scanf("%d%d",&n,&m);
    for (int i=2;i<=n;i++)
    {
        int u;
        scanf("%d",&u);
        add(u,i);
    }
    scanf("%s",c+1);  //////
    for (int i=1;i<=m;i++)   //把询问连起来,方便一个子树中的所有询问一起处理 
    {
        int u,w;
        scanf("%d%d",&u,&w);
        addq(u,w,i);
    }
    getson(1,1);
    dfs(1,0);
    for (int i=1;i<=m;i++) 
        if (ans[i]) printf("Yes
");
        else printf("No
");
    return 0;
}
原文地址:https://www.cnblogs.com/wutongtong3117/p/7673387.html