Submit | All submissions | Best solutions | Back to list |
RTREE - Roger and tree |
Roger is a computer science student who likes connected undirected acyclic graphs, also known as trees. He especially likes solving problems about trees. Recently Roger found a piece of paper with a rooted tree with 'N' vertices drawn on it (numbered from 1 to 'N'). He also found 'Q' queries on the same piece of paper, where each query was an integer 'S' between 1 to 'N'. But the paper said nothing about the description of the queries. So he decided to find the longest path of each of the subtree 'S'.
Roger spent two sleepless nights trying to solve this problem efficiently. He is still trying and won't sleep until he knows the answer to each query. Write a program which answers all the queries correctly.
Input
The first line contains an integer 'N', then N-1 lines follow.
Each of the next 'N-1' line contains two integer 'U' and 'V' which means that vertex 'U' and 'V' are connected.
Next line contains an integer 'R' which denotes the root of the tree.
Next line contains another integer 'Q' which denotes the number of queries.
Each of the next 'Q' line contains an integer 'S' between (1 to N).
Output
For each query print the longest path of the subtree 'S' rooted at vertex 'R'.
Output exactly 'Q' lines, each line containing the output of the ith query.
Example
SAMPLE INPUT 3 1 2 1 3 1 2 1 2 SAMPLE OUTPUT 2 0
Constraints
1 ≤ N ≤ 10^5
1 ≤ U,V ≤ N
1 ≤ R ≤ N
1 ≤ Q ≤ 10^5
1 ≤ S ≤ N
Like Trees? Try the problems: RTREE2, RTREE3 as well
Added by: | Rana Saha |
Date: | 2014-08-26 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All |
Resource: | Own problem (Codecracker 2014) |
hide comments
|
||||||
2021-12-11 11:10:32
50th testcase failed :( |
||||||
2021-09-28 08:44:15
Really don't know why I get RE(SIGSEGV)... Last edit: 2021-09-28 08:44:31 |
||||||
2018-11-29 15:07:24
There can be many reasons for getting wrong answer on 50th test case.I got 3 wrong answer on 50th test case and all the 3 times the reasons were different.Even a silly program is running till 50 th test case and then giving wrong answer and remember 1 thing you can get longest path from smaller subtree also. For example : 10 1 2 1 3 2 4 2 5 2 6 5 7 6 8 7 9 8 10 1 2 1 2 answer 6 //(not 5) 6 Last edit: 2018-11-29 15:07:52 |
||||||
2018-07-05 08:12:13
used same code for atleast 4 problems xD. AC in 1 go :D |
||||||
2018-04-26 18:34:56
DFS + DP Last edit: 2018-04-26 18:40:37 |
||||||
2018-02-02 08:58:26
on the first go::come on 47 |
||||||
2017-10-19 22:51:58
what's wrong with 50th case??? anyone please :( Last edit: 2017-10-20 00:06:42 |
||||||
2017-09-27 11:43:50
Unable to resolve WA on 50th test case :( any hints from those who solved ? :/ |
||||||
2017-03-19 19:15:15 spoj
For ppl getting wrong answer on 50th test case, take max size of array to be 1000010..This helped me!!! |
||||||
2017-03-09 18:02:53
Getting wrong answer on 50th test case??? please help |