結果

問題 No.2337 Equidistant
ユーザー lam6er
提出日時 2025-04-15 21:53:28
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,340 bytes
コンパイル時間 459 ms
コンパイル使用メモリ 82,096 KB
実行使用メモリ 206,552 KB
最終ジャッジ日時 2025-04-15 21:55:17
合計ジャッジ時間 18,790 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 1 WA * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from sys import stdin
import sys
sys.setrecursionlimit(1 << 25)
input = sys.stdin.read
data = input().split()
idx = 0

def main():
    global idx
    N, Q = int(data[idx]), int(data[idx+1])
    idx += 2

    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a = int(data[idx])
        b = int(data[idx+1])
        adj[a].append(b)
        adj[b].append(a)
        idx += 2

    parent = [0] * (N + 1)
    depth = [0] * (N + 1)
    size = [1] * (N + 1)
    visited = [False] * (N + 1)
    stack = [(1, False)]
    parent[1] = 0

    while stack:
        node, vis = stack.pop()
        if vis:
            for neighbor in adj[node]:
                if neighbor != parent[node]:
                    size[node] += size[neighbor]
            continue
        visited[node] = True
        stack.append((node, True))
        for neighbor in reversed(adj[node]):
            if not visited[neighbor] and neighbor != parent[node]:
                parent[neighbor] = node
                depth[neighbor] = depth[node] + 1
                stack.append((neighbor, False))

    LOG = 20
    binary_lift = [[0] * (N + 1) for _ in range(LOG)]
    binary_lift[0] = parent[:]

    for k in range(1, LOG):
        for v in range(1, N + 1):
            binary_lift[k][v] = binary_lift[k-1][binary_lift[k-1][v]]

    def get_kth_ancestor(u, k):
        if k < 0:
            return -1
        current = u
        for i in range(LOG):
            if k & (1 << i):
                current = binary_lift[i][current]
                if current == 0:
                    return -1
        return current

    def lca(u, v):
        if depth[u] < depth[v]:
            u, v = v, u
        for i in range(LOG-1, -1, -1):
            if depth[u] - (1 << i) >= depth[v]:
                u = binary_lift[i][u]
        if u == v:
            return u
        for i in range(LOG-1, -1, -1):
            if binary_lift[i][u] != binary_lift[i][v]:
                u = binary_lift[i][u]
                v = binary_lift[i][v]
        return parent[u]

    output = []
    for _ in range(Q):
        S = int(data[idx])
        T = int(data[idx+1])
        idx += 2
        if S == T:
            output.append(0)
            continue
        l = lca(S, T)
        a = depth[S] - depth[l]
        b = depth[T] - depth[l]
        D = a + b
        if D % 2 != 0:
            output.append(0)
            continue
        k = D // 2
        if k <= a:
            M = get_kth_ancestor(S, k)
        else:
            steps_from_T = b - (k - a)
            M = get_kth_ancestor(T, steps_from_T)
        
        steps_S = depth[S] - (depth[M] + 1)
        if steps_S < 0:
            child_S = S
        else:
            child_S = get_kth_ancestor(S, steps_S)
        
        steps_T = depth[T] - (depth[M] + 1)
        if steps_T < 0:
            child_T = T
        else:
            child_T = get_kth_ancestor(T, steps_T)
        
        if child_S == 0 or child_S == -1:
            size_cs = 0
        else:
            size_cs = size[child_S]
        
        if child_T == 0 or child_T == -1:
            size_ct = 0
        else:
            size_ct = size[child_T]
        
        ans = size[M] - size_cs - size_ct
        output.append(ans if ans >=0 else 0)
    
    print('\n'.join(map(str, output)))

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