Codeforces 570 D. Tree Requests +dfs Search order

Source: Internet
Author: User

Links: Http://codeforces.com/problemset/problem/570/D


D. Tree requeststime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Roman planted a tree consisting ofNVertices. Each vertex contains a lowercase 中文版. Vertex1is the root of the tree, and each of the n?-? 1Remaining vertices has aParentIn the tree. Vertex is connected with the parent by an edge. The parent of vertexIis 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 in the path from the root to v along the edges. In particular, the depth of the root was equal to 1.

We say that vertexuis in thesubtreeof vertexv, if we can get fromuTov, moving from the vertex to the parent. In particular, vertexvis in its subtree.

Roma gives youmQueries, theI-th of which consists of the 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 apalindrome. The letters that is written in the vertexes, can is rearranged in any order to make a palindrome, but all letters should be used.

Input

The first line contains integers n, m (1?≤? N,? M.≤?500?000)-the number of nodes in the tree and queries, respectively.

The following line contains n?-? 1Integers P2,? P 3,?...,? P N -the parents of vertices from the second to theN-th (1?≤? P i? < I ).

The next line contains n lowercase 中文版 letters, the i-th of these letters is written on vertex i .

NextmLines describe the queries, theI-th line contains and numbers vi , Hi (1?≤? v i,? h i? ≤? N )-the vertex and the depth that appear in theI-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 Palindro Me from the letters written on the vertices, otherwise print "No" (without the quotes).

Sample Test (s) input
6 1 1 3 3zacccd1 13 34 16 11 2
Output
Yesnoyesyesyes
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 on 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".



Test instructions

To tell you the parent-child relationship of a tree, the 1 node is the root. I'll tell you the letters at every point.

Ask if the letters in the node H of the V-node tree (which contains the V nodes) can form a palindrome string.


Practice:

Use DFS search to mark all nodes with the left and right labels first. After such marking. Each node is labeled with a left label as own new label. Then the new label of all the nodes of the subtree must be between the left and right markings of the child tree root node.

The label is then layered to do.

Each of the letters is counted separately.

The left label of all nodes in the layer is +1 in the tree array. Then, for all queries on that layer, do a tree-like array statistic. (SUM (rit[v])-sum (lft[v]-1)).

Suppose it is odd to say that the letter has an odd number within the range of the query.

Each query has a maximum of one odd number of letters. Otherwise, it does not constitute a palindrome string


#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector > #include <math.h>using namespace std;const int N = 500100;int f[n];vector<int> son[n];int id;int Lft[N], Rit[n];int Deps[n]; int Ans[n];char str[n];vector<int> g[n];//depth vector<pair<int,int> > q[n];void dfs (int nw,int dep) {LFT [nw]=id++;d EPS[NW]=DEP; G[dep].push_back (NW); for (int i=0;i<son[nw].size (); i++) {int to=son[nw][i];d fs (to,dep+1);} rit[nw]=id++;} int bit[2*n];int lowbit (int x) {return x& (-X);} void Add (int wei,int x) {while (Wei<=id) {bit[wei]+=x;wei+=lowbit (Wei);}} int sum (int wei) {if (wei==0) return 0;int sum=0;while (wei>0) {sum+=bit[wei];wei-=lowbit (Wei);} return sum;} int main () {int n,m;while (scanf ("%d%d", &n,&m)!=eof) {for (int i=2;i<=n;i++) {scanf ("%d", f+i); Son[f[i]]. Push_back (i);} scanf ("%s", str+1), Id=1;dfs (n), int dep=1;for (int i=1;i<=m;i++) {int vv,hh;scanf ("%d%d", &vv,&hh);d ep= Max (HH,DEP); Q[hh].push_back (make_pair<int,int> (Vv,i));} for (int i=1;i<=dep;i++) {for (int j=0;j<26;j++) {if (j==25) int kkk=1;for (int k=0;k<g[i].size (); k++)//per node {if (str[g[i][k]]-' a ' ==j) add (lft[g[i][k]],1);} for (int k=0;k<q[i].size (); k++) {int v=q[i][k].first;int ii=q[i][k].second;if ((SUM (rit[v])-sum (lft[v]-1)) &1) ans[ii]++;} for (int k=0;k<g[i].size (); k++)//per node {if (str[g[i][k]]-' a ' ==j) add (lft[g[i][k]],-1);} printf ("JJ%d \ n", j);} printf ("II%d \ n", i);} for (int i=1;i<=m;i++) {if (ans[i]<=1) printf ("yes\n"); elseprintf ("no\n");}} return 0;}




Codeforces 570 D. Tree Requests +dfs Search order

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.