Codeforces 570 D. Tree Requests +dfs Search order

Source: Internet
Author: User
Tags lowercase printf time limit

Links: Http://

D. Tree requests time limit per test 2 seconds memory limit per test megabytes input standard input Output standard OU Tput

Roman planted a tree consisting of n vertices. Each vertex contains a lowercase 中文版. Vertex 1 is the root of the tree, and each of the n-1 remaining vertices have a parent in the tree. Vertex is connected with the parent by an edge. The parent of vertex i is vertex pi, the parent index was 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 vertex u are in the subtree of Vertex V, if we can get from U-V, moving from the vertex to the parent. In particular, Vertex v are in the its subtree.

Roma gives you m queries, the i-th of which consists of the numbers VI, HI. Let's consider the vertices in the subtree VI located at DEPTHHI. Determine whether you can use the letters written at these vertices to make a string, which is a palindrome. 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)-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 & Lt i).

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

Next m lines describe the queries, the i-th line contains the numbers VI, HI (1≤vi, hi≤n)-the vertex and the depth t Hat 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 palindrome from the letters written On the vertices, otherwise print "No" (without the quotes). Sample Test (s) input

6 5
1 1 1 3 3
1 1
3 3
4 1
6 1
1 2
Yes No Yes Yes Yes


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

Tell you the parent-child relationship of a tree, the 1 node is the root, and then the letters on each point are told.

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


Use DFS search to label all nodes with left and right labels first. After such marking, each node is labeled with the left as itself a 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 layer is counted separately for each letter.

The left label of all nodes in the layer is +1 in the tree array. The tree array is then counted for all queries for that layer (sum (rit[v])-sum (lft[v]-1)).

If it is odd, the letter has an odd number within the range of the query.

Each query has a maximum of an 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++;
	G[dep].push_back (NW);
		for (int i=0;i<son[nw].size (); i++) {int to=son[nw][i];
	DFS (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 (a);
		int dep=1;
			for (int i=1;i<=m;i++) {int vv,hh;
			scanf ("%d%d", &vv,&hh);
			Dep=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");
		else printf ("no\n");
}} return 0; }

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