570D - Tree Requests

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

题意:让你在以root 为父节点的子树中挑出所有深度为d的点问这些节点上的字母经过任意排列是不是可以形成一个回文。
思路:由于是任意组合,那么只要这个单词数量是偶数就必定可以,如果奇数个数的单词数不超过1个,那么也是可以的。由于是离线查询,所以可以想到用dsu on tree 去统计树上节点的属性,这个属性就是深度为d各个单词的数量。。。我把以u节点的所有深度存下来再去查询错了。。把所有查询先读下来边统计边处理查询再一次输出对了。。。还是玄学。。。

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<vector>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<map>
using namespace std;

//thanks to pyf ...

#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
typedef pair<int, int> PII;
typedef long long ll;

const int N = 500000 + 5;

struct Edge
{
    int u, v, next;
} edge[N * 2];

char cap[N];
int cur_deep_cnt[N];
int cur_deep_dif_cnt[N][30];
int cur_status[N];
int ans[N];
vector<PII>Q[N];
int deep[N];
int clr[N];
int big[N];
int head[N];
int sz[N];
int tot;
int Max_d = 0;
void init()
{
    CLR(cur_deep_cnt, 0);
    CLR(cur_deep_dif_cnt, 0);
    CLR(big, 0);
    CLR(head, -1);
    CLR(sz, 0);
    CLR(clr, 0);
    tot = 0;
    Max_d = 0;
}
void add_edge(int u, int v)
{
    edge[tot].u = u;
    edge[tot].v = v;
    edge[tot].next = head[u] ;
    head[u] = tot ++ ;
}
void get_sz(int u, int fa, int d)
{
    sz[u] = 1;
    deep[u] = d;
    Max_d = max(Max_d, d);
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].v;
        if (v == fa)
            continue;
        get_sz(v, u, d + 1);
        sz[u] += sz[v];
    }
}
// void add(int u, int fa)
// {
//  int val = cap[u] - 'a';
//  cur_deep_cnt[deep[u]] -= cur_deep_dif_cnt[deep[u]][val];
//  cur_deep_dif_cnt[deep[u]][val] ^= 1;
//  cur_deep_cnt[deep[u]] += cur_deep_dif_cnt[deep[u]][val];
//  for (int i = head[u]; i != -1; i = edge[i].next)
//  {
//      int v = edge[i].v;
//      if (fa == v || big[v])
//          continue;
//      add(v, u);
//  }
// }
void add(int u, int fa)
{
    cur_status[deep[u]] ^= (1 << (cap[u] - 'a'));
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].v;
        if (big[v] || v == fa)
            continue;
        add(v, u);
    }
}
void dfs(int u, int fa, bool keep)
{
    int mx = -1, big_son = -1;
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].v;
        if (v == fa)
            continue;
        if (sz[v] > mx)
            mx = sz[v] , big_son = v;
    }
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        if (edge[i].v != fa && edge[i].v != big_son)
        {
            dfs(edge[i].v, u, 0);
        }
    }
    if (big_son != -1)
        dfs(big_son, u, 1), big[big_son] = 1;
    add(u, fa);
    for (int i = 0; i < Q[u].size(); i++)
    {
        int id = Q[u][i].second;
        int dep = Q[u][i].first;
        //  ans[id] = (cur_deep_cnt[dep] > 1);
        ans[id] = (cur_status[dep] & (cur_status[dep] - 1)) > 1;
    }
    if (big_son != -1)
        big[big_son] = 0;
    if (!keep)
        add(u, fa);
}

int main()
{
    int n, m;
    while (cin >> n >> m)
    {
        init();
        for (int i = 2; i <= n; i++)
        {
            int u;
            scanf("%d", &u);
            add_edge(u, i);
//          add_edge(i, u);
        }
        for (int i = 1; i <= n; i++)
            cin >> cap[i];
        // for (int i = 1; i <= n; i++)
        // {
        //  for (int j = 1; j <= Max_d; j++)
        //  {
        //      cout << ans[i][j - 1] << " ";
        //  }
        //  cout << endl;
        // }
        for (int i = 0; i < m; i++)
        {
            int r, d;
            scanf("%d%d", &r, &d);
            Q[r].push_back(mp(d, i));
        }
        get_sz(1, 1, 1);
        dfs(1, 1, 0);
        for (int i = 0; i < m; i++)
            if (ans[i])
                printf("No\n");
            else
                printf("Yes\n");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值