結果
| 問題 |
No.2337 Equidistant
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-06-02 22:21:44 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,836 bytes |
| コンパイル時間 | 690 ms |
| コンパイル使用メモリ | 82,176 KB |
| 実行使用メモリ | 193,488 KB |
| 最終ジャッジ日時 | 2024-12-28 19:16:53 |
| 合計ジャッジ時間 | 37,767 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 3 WA * 25 |
ソースコード
import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random
#sys.setrecursionlimit(10**9)
#n = int(input())
#alist = list(map(int,input().split()))
hen = collections.defaultdict(list)
#s = input()
n,q = map(int,input().split())
ansestor = [[-1 for i in range(n)] for j in range(math.floor(math.log2(n)) + 1)]
for i in range(n-1):
# alist.append(list(map(int,input().split())))
u,v = map(int,input().split())
u-=1
v-=1
hen[u].append(v)
hen[v].append(u)
#dp = [[0]*n for i in range(m)]
dist = [10**10] * n
dist[0] = 0
d = collections.deque()
d.append(0)
seen = set()
seen.add(0)
while d:
now = d.popleft()
for i in hen[now]:
if i not in seen:
ansestor[0][i] = now
d.append(i)
dist[i] = dist[now] + 1
seen.add(i)
for i in range(math.floor(math.log2(n))):
for j in range(n):
if ansestor[i][j] == -1:
ansestor[i+1][j] = -1
else:
ansestor[i+1][j] = ansestor[i][ansestor[i][j]]
def ans(u,a):
for i in reversed(range(math.floor(math.log2(n)))):
if u == -1:
break
if (a >> i) & 1:
u = ansestor[i][u]
return u
for i in range(q):
s,t = map(int,input().split())
s-=1
t-=1
sk = s
st = t
if dist[s] > dist[t]:
s = ans(s,dist[s]-dist[t])
if dist[s] < dist[t]:
t = ans(t,-dist[s]+dist[t])
for i in range(math.floor(math.log2(n)),-1,-1):
ns = ansestor[i][s]
nt = ansestor[i][t]
if ns != nt:
s = ns
t = nt
x = ansestor[0][s]
if dist[sk] == dist[st]:
print(dist[x] + 1)
else:
z = dist[sk] - dist[x] + dist[st] - dist[x]
if z % 2 == 0:
print(1)
else:
print(0)