POLICEMEN - Police Men

There is a country of n cities and n-1 bidirectional roads you can go from any city to another using its roads

You are a police man and there is a theif who is going to escape from the country using an airport in city 1 you are given m queries which of type "a b" which means the thief is in city a and you are in city b you should find if it's possible to catch him before he escape or not and find the city which you will catch him in if it's possible.

You are moving at the same speed the thief moving at and the thief is taking the shortest path to city 1

If you arrived in a city in the same time as the thief you can catch him in it if you arrived before him you can wait for him

If there is more than one city you can catch him in print the nearest one to you.


the first line contains an integer n (1 n 104)  then n-1 lines each contains two integers which means there is a road between city u and v

then an intger q (1 ≤ q 104) and q lines of form a b (1 ≤ a , b n) which are the thief's position and your position


for each query print YES then a space then the city which you will catch him in if it's possible otherwise print NO


1 2
1 3
2 4
4 5
1 2
1 1
5 4
2 3
Output: NO

hide comments
vinay1998: 2018-06-13 08:46:26

AC in one go !!! ( LCA , shortest distance from root )

abdou_93: 2016-08-12 08:47:24

"nearest one to you."

mahmoudbadawy5: 2016-07-25 08:18:21

Sorry but there were a mistake in the data set but I couldn't rejudge the solutions please resubmit your solutions

Added by:mahmoudbadawy5
Time limit:2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU