結果

問題 No.2337 Equidistant
ユーザー lam6er
提出日時 2025-04-16 00:58:19
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,611 bytes
コンパイル時間 140 ms
コンパイル使用メモリ 82,700 KB
実行使用メモリ 577,120 KB
最終ジャッジ日時 2025-04-16 00:59:47
合計ジャッジ時間 26,384 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 5 WA * 22 MLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(1 << 25)

def main():
    import sys
    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
    
    parent = [0] * (N + 1)
    depth = [0] * (N + 1)
    in_time = [0] * (N + 1)
    out_time = [0] * (N + 1)
    sz = [1] * (N + 1)
    children = [[] for _ in range(N + 1)]
    timer = 1

    def dfs(u, p):
        nonlocal timer
        in_time[u] = timer
        timer += 1
        parent[u] = p
        for v in adj[u]:
            if v != p:
                depth[v] = depth[u] + 1
                dfs(v, u)
                sz[u] += sz[v]
                children[u].append(v)
        out_time[u] = timer - 1

    dfs(1, 0)

    for u in range(1, N + 1):
        children[u].sort(key=lambda x: in_time[x])

    LOG = 20
    up = [[-1] * (N + 1) for _ in range(LOG)]
    up[0] = parent[:]
    for k in range(1, LOG):
        for u in range(1, N + 1):
            if up[k-1][u] != -1:
                up[k][u] = up[k-1][up[k-1][u]]

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

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

    def get_ancestor_at_depth(u, d):
        if depth[u] < d:
            return -1
        delta = depth[u] - d
        return get_kth_ancestor(u, delta)

    for _ in range(Q):
        S = int(data[idx])
        T = int(data[idx+1])
        idx += 2
        if S == T:
            print(0)
            continue
        lca_node = lca(S, T)
        a = depth[S] - depth[lca_node]
        b = depth[T] - depth[lca_node]
        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
            target_depth = depth[lca_node] + m
            M = get_ancestor_at_depth(T, target_depth)
        if M == -1 or M == 0:
            print(0)
            continue
        cs = children[M]
        s_in = in_time[S]
        u = -1
        left, right = 0, len(cs) - 1
        while left <= right:
            mid = (left + right) // 2
            c = cs[mid]
            if in_time[c] <= s_in <= out_time[c]:
                u = c
                break
            elif in_time[c] > s_in:
                right = mid - 1
            else:
                left = mid + 1
        u_sz = sz[u] if u != -1 else 0
        t_in = in_time[T]
        v = -1
        left, right = 0, len(cs) - 1
        while left <= right:
            mid = (left + right) // 2
            c = cs[mid]
            if in_time[c] <= t_in <= out_time[c]:
                v = c
                break
            elif in_time[c] > t_in:
                right = mid - 1
            else:
                left = mid + 1
        v_sz = sz[v] if v != -1 else 0
        ans = sz[M] - u_sz - v_sz
        print(ans)

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