結果

問題 No.2337 Equidistant
ユーザー gew1fw
提出日時 2025-06-12 17:58:18
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,882 bytes
コンパイル時間 181 ms
コンパイル使用メモリ 82,228 KB
実行使用メモリ 200,660 KB
最終ジャッジ日時 2025-06-12 17:58:42
合計ジャッジ時間 21,168 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other WA * 21 TLE * 1 -- * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from sys import stdin
from collections import deque
sys.setrecursionlimit(1 << 25)

def main():
    input = sys.stdin.read
    data = input().split()
    idx = 0
    N, Q = int(data[idx]), int(data[idx+1])
    idx +=2
    edges = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a = int(data[idx])
        b = int(data[idx+1])
        edges[a].append(b)
        edges[b].append(a)
        idx +=2
    
    LOG = 20
    up = [[0]*(N+1) for _ in range(LOG)]
    depth = [0]*(N+1)
    parent = [0]*(N+1)
    in_time = [0]*(N+1)
    out_time = [0]*(N+1)
    size = [1]*(N+1)
    time_stamp = 1
    
    stack = [(1, 0, False)]
    while stack:
        u, p, visited = stack.pop()
        if visited:
            for v in edges[u]:
                if v != p:
                    size[u] += size[v]
            out_time[u] = time_stamp
            time_stamp +=1
            continue
        parent[u] = p
        depth[u] = depth[p] +1
        in_time[u] = time_stamp
        time_stamp +=1
        stack.append((u, p, True))
        for v in edges[u]:
            if v != p:
                stack.append((v, u, False))
    
    up[0] = parent[:]
    for k in range(1, LOG):
        for v in range(1, N+1):
            up[k][v] = up[k-1][up[k-1][v]]
    
    def lca(u, v):
        if depth[u] < depth[v]:
            u, v = v, u
        for k in reversed(range(LOG)):
            if depth[u] - (1<<k) >= depth[v]:
                u = up[k][u]
        if u == v:
            return u
        for k in reversed(range(LOG)):
            if up[k][u] != up[k][v]:
                u = up[k][u]
                v = up[k][v]
        return parent[u]
    
    def get_kth_ancestor(u, k):
        for i in range(LOG):
            if k & (1<<i):
                u = up[i][u]
        return u
    
    for _ in range(Q):
        S = int(data[idx])
        T = int(data[idx+1])
        idx +=2
        if S == T:
            print(0)
            continue
        L = lca(S, T)
        a = depth[S] - depth[L]
        b = depth[T] - depth[L]
        D = a + b
        if D %2 !=0:
            print(0)
            continue
        K = D//2
        if K <= a:
            M = get_kth_ancestor(S, K)
        else:
            m = K - a
            steps = b - m
            M = get_kth_ancestor(T, steps)
        u = None
        for c in edges[M]:
            if c == parent[M]:
                continue
            if in_time[c] <= in_time[S] <= out_time[c]:
                u = c
                break
        v = None
        for c in edges[M]:
            if c == parent[M]:
                continue
            if in_time[c] <= in_time[T] <= out_time[c]:
                v = c
                break
        if u is None or v is None:
            print(0)
        else:
            ans = size[M] - size[u] - size[v]
            print(ans)

if __name__ == '__main__':
    main()
0