Google Online Assessment GOCC | Find Palindromes
Anonymous User
947
You are given a tree T and N nodes and the tree rooted at node 1. Every node has character C[i] assigned to it. You are given Q queries of the 
following format:
	Query : u
You must find whether string S that is generated for the node u is palindromic or not.String S for node u is generated as follows:
	
	S = Empty
	makeString(u)
	{
		for all v = child of u
			makeString(v)
		S += c[u]
	}
	
	/*  Child of v should be traversed in order of increasing node number */ 
	
	Input format :
	1. The first line contains an integer N 
	2. Next N-1 lines contain two space-separated integers 'u' and 'v' denoting and egde between node u and node v.
	3. The next line contains N space-separated characters denoting C[i] for all i fro 1 to N.
	4. Next line contains integer Q.
	5. Next Q lines contain an integer 'u' denoting the node for the query. 
	
	Output format :
	Print Q lines and in each line print 1 if S is palindrome. Otherwise 0.
	
	Constraints :
	1 <= N,Q <= 200000
	C[i] contains lowercase latin letters
	
	Sample input:
	5
	1 2
	1 3
	2 4
	2 5
	a b a b c
	2
	1
	2
	
	Sample Output:
	0
	1
	
	Explanation:
	for query 1 the string is "bcbaa" so not a palindrome
	for query 2 the string is "bcb" so it is a palindrome	
	

How to solve this ?

Comments (3)