結果

問題 No.2337 Equidistant
ユーザー lam6er
提出日時 2025-04-16 00:59:44
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,127 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 81,140 KB
実行使用メモリ 227,196 KB
最終ジャッジ日時 2025-04-16 01:01:24
合計ジャッジ時間 30,089 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 6 WA * 22
権限があれば一括ダウンロードができます

ソースコード

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
    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
    
    LOG = 20
    up = [[-1]*(LOG) for _ in range(N+1)]
    depth = [0]*(N+1)
    size = [1]*(N+1)
    parent = [0]*(N+1)
    
    stack = [(1, -1)]
    while stack:
        u, p = stack.pop()
        parent[u] = p
        for v in adj[u]:
            if v != p:
                depth[v] = depth[u] +1
                up[v][0] = u
                stack.append((v, u))
    
    for k in range(1, LOG):
        for v in range(1, N+1):
            if up[v][k-1] != -1:
                up[v][k] = up[up[v][k-1]][k-1]
            else:
                up[v][k] = -1
    
    stack = [(1, -1)]
    post_order = []
    while stack:
        u, p = stack.pop()
        post_order.append(u)
        for v in adj[u]:
            if v != p:
                stack.append((v, u))
    
    for u in reversed(post_order):
        if parent[u] != -1:
            size[parent[u]] += size[u]
    
    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[u][k]
        if u == v:
            return u
        for k in reversed(range(LOG)):
            if up[u][k] != up[v][k]:
                u = up[u][k]
                v = up[v][k]
        return parent[u]
    
    def get_kth_ancestor(u, k):
        if k <0:
            return -1
        for i in range(LOG):
            if k & (1 << i):
                u = up[u][i]
                if u == -1:
                    return -1
        return u
    
    def is_ancestor(u, v):
        return lca(u, v) == u
    
    out = []
    for _ in range(Q):
        S = int(data[idx])
        T = int(data[idx+1])
        idx +=2
        L = lca(S, T)
        a = depth[S] - depth[L]
        b = depth[T] - depth[L]
        D = a + b
        if D %2 !=0:
            out.append('0')
            continue
        k = D //2
        if k <= a:
            M = get_kth_ancestor(S, k)
        else:
            rem = k -a
            M = get_kth_ancestor(T, b - rem)
        
        subtract_c =0
        if is_ancestor(M, S):
            k_s = depth[S] - depth[M]
            steps = k_s -1
            if steps >=0:
                c = get_kth_ancestor(S, steps)
            else:
                c = S
            subtract_c = size[c]
        
        subtract_d =0
        if is_ancestor(M, T):
            k_t = depth[T] - depth[M]
            steps = k_t -1
            if steps >=0:
                d = get_kth_ancestor(T, steps)
            else:
                d = T
            subtract_d = size[d]
        
        ans = size[M] - subtract_c - subtract_d
        out.append(str(ans))
    
    print('\n'.join(out))

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