結果

問題 No.2337 Equidistant
ユーザー hato336hato336
提出日時 2023-06-02 23:09:13
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,834 bytes
コンパイル時間 800 ms
コンパイル使用メモリ 86,828 KB
実行使用メモリ 198,832 KB
最終ジャッジ日時 2023-08-28 05:40:35
合計ジャッジ時間 38,541 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 239 ms
92,500 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 1,756 ms
179,216 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 1,906 ms
184,864 KB
testcase_25 WA -
testcase_26 AC 1,851 ms
182,952 KB
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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)+1))):
        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)
0