Caso # | Resultado | Tiempo | Memoria |
---|---|---|---|
#1 |
Correcto
|
0.029 s | 3 KBi |
#2 |
Correcto
|
0.025 s | 3 KBi |
#3 |
Correcto
|
0.022 s | 3 KBi |
#4 |
Correcto
|
0.025 s | 3 KBi |
#5 |
Correcto
|
0.02 s | 3 KBi |
#6 |
Correcto
|
0.02 s | 3 KBi |
#7 |
Correcto
|
0.018 s | 3 KBi |
#8 |
Correcto
|
0.018 s | 3 KBi |
#9 |
Correcto
|
0.019 s | 3 KBi |
#10 |
Correcto
|
0.033 s | 3 KBi |
#11 |
Correcto
|
0.019 s | 3 KBi |
#12 |
Correcto
|
1.175 s | 22 KBi |
#13 |
Correcto
|
1.184 s | 22 KBi |
#14 |
Correcto
|
0.932 s | 22 KBi |
#15 |
Correcto
|
0.87 s | 22 KBi |
#16 |
Correcto
|
1.028 s | 31 KBi |
#17 |
Correcto
|
0.915 s | 26 KBi |
#18 |
Correcto
|
0.933 s | 26 KBi |
#19 |
Correcto
|
1.025 s | 25 KBi |
#20 |
Correcto
|
1.066 s | 25 KBi |
N = int(input()) g = [[] for _ in range(N)] time_in = [None for _ in range(N)] time_out = [None for _ in range(N)] for _ in range(N - 1): p, n = map(int, input().split()) g[p].append(n) def dfs(node): s = [[node, 0]] time = time_in[node] = 0 while s: node, i = s[-1] # if it still # has children # then add them if i < len(g[node]): s[-1][1] += 1 s.append([g[node][i], 0]) time_in[g[node][i]] = time + 1 else: s.pop() time_out[node] = time time += 1 def query(a, b): return time_in[a] <= time_in[b] <= time_out[a] dfs(0) Q = int(input()) for _ in range(Q): a, b = map(int, input().split(" ")) print("Yes" if query(a, b) else "No")