結果

問題 No.2337 Equidistant
ユーザー mkawa2mkawa2
提出日時 2024-09-10 21:33:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,283 ms / 4,000 ms
コード長 2,633 bytes
コンパイル時間 486 ms
コンパイル使用メモリ 82,556 KB
実行使用メモリ 146,992 KB
最終ジャッジ日時 2024-09-10 21:34:10
合計ジャッジ時間 21,503 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,076 KB
testcase_01 AC 37 ms
54,428 KB
testcase_02 AC 37 ms
54,368 KB
testcase_03 AC 37 ms
54,152 KB
testcase_04 AC 38 ms
53,160 KB
testcase_05 AC 37 ms
53,556 KB
testcase_06 AC 132 ms
78,016 KB
testcase_07 AC 125 ms
78,232 KB
testcase_08 AC 123 ms
77,696 KB
testcase_09 AC 123 ms
77,988 KB
testcase_10 AC 129 ms
78,056 KB
testcase_11 AC 876 ms
127,632 KB
testcase_12 AC 841 ms
127,440 KB
testcase_13 AC 859 ms
127,720 KB
testcase_14 AC 840 ms
127,644 KB
testcase_15 AC 860 ms
127,568 KB
testcase_16 AC 842 ms
127,656 KB
testcase_17 AC 886 ms
127,364 KB
testcase_18 AC 876 ms
127,536 KB
testcase_19 AC 837 ms
127,564 KB
testcase_20 AC 863 ms
127,736 KB
testcase_21 AC 950 ms
146,992 KB
testcase_22 AC 550 ms
129,440 KB
testcase_23 AC 690 ms
127,472 KB
testcase_24 AC 1,193 ms
146,524 KB
testcase_25 AC 780 ms
129,416 KB
testcase_26 AC 1,283 ms
146,892 KB
testcase_27 AC 684 ms
129,060 KB
testcase_28 AC 706 ms
129,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 62)

# md = 10**9+7
md = 998244353

class LCA:
    def __init__(self, parent, depth):
        n = len(parent)
        self._depth = depth
        log = max(self._depth).bit_length()
        self._table = [parent]+[[-1]*n for _ in range(log)]
        row0 = self._table[0]
        for lv in range(log):
            row1 = self._table[lv+1]
            for u in range(n):
                if row0[u] == -1: continue
                row1[u] = row0[row0[u]]
            row0 = row1

    def anc(self, u, v):
        diff = self._depth[u]-self._depth[v]
        if diff < 0: u, v = v, u
        diff = abs(diff)
        u = self.up(u, diff)
        if u == v: return u,-1,-1
        for lv in range(self._depth[u].bit_length()-1, -1, -1):
            anclv = self._table[lv]
            if anclv[u] != anclv[v]: u, v = anclv[u], anclv[v]
        return self._table[0][u],u,v

    def up(self, u, dist):
        lv = 0
        while dist and u != -1:
            if dist & 1: u = self._table[lv][u]
            lv, dist = lv+1, dist >> 1
        return u

n,q=LI()
to=[[] for _ in range(n)]
for _ in range(n-1):
    u,v=LI1()
    to[u].append(v)
    to[v].append(u)

def dfs(root=0):
    uu, stack = [], [root]
    while stack:
        u = stack.pop()
        uu.append(u)
        for v in to[u]:
            if v == parent[u]: continue
            parent[v] = u
            depth[v] = depth[u]+1
            stack.append(v)
    return uu

parent, depth = [-1]*n, [0]*n
uu = dfs()
dp=[1]*n
for u in uu[:0:-1]:
    p=parent[u]
    dp[p]+=dp[u]

lca=LCA(parent,depth)
for _ in range(q):
    u,v=LI1()
    a,pu,pv=lca.anc(u,v)
    d=depth[u]+depth[v]-depth[a]*2
    if d&1:
        print(0)
    elif depth[u]==depth[v]:
        print(n-dp[pu]-dp[pv])
    else:
        if depth[u]>depth[v]:
            u,v=v,u
            pu,pv=pv,pu
        pv=lca.up(v,d//2-1)
        m=parent[pv]
        print(dp[m]-dp[pv])
0