Who's your daddy?

4

4 votes
Easy-Medium
Problem

Given an undirected rooted tree at node 1, consisting of N nodes. Find the kth ancestor of a given node. There will be Q queries.

Input:

First line contains N, the number of nodes in the tree. Next N - 1 lines contains two integers u and v, there is an edge between u and v. Next line contains Q, the number of queries. Each of the next Q lines contains a node p and and integer k.

Output:

Print on a single line the answer of query as described.

Constraints:

1N,k,p105

1Q105

1u,v105

Update: In case No ancestor exists, output root node i.e, 1.

Problem Setter - Saurabh

Time Limit: 1
Memory Limit: 256
Source Limit:
Editor Image

?