結果

問題 No.2337 Equidistant
ユーザー rlangevinrlangevin
提出日時 2024-01-29 21:34:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,679 ms / 4,000 ms
コード長 3,114 bytes
コンパイル時間 1,647 ms
コンパイル使用メモリ 81,444 KB
実行使用メモリ 159,680 KB
最終ジャッジ日時 2024-01-29 21:35:13
合計ジャッジ時間 26,826 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 36 ms
53,460 KB
testcase_03 AC 36 ms
53,460 KB
testcase_04 AC 36 ms
53,460 KB
testcase_05 AC 36 ms
53,460 KB
testcase_06 AC 125 ms
77,440 KB
testcase_07 AC 127 ms
77,352 KB
testcase_08 AC 131 ms
77,608 KB
testcase_09 AC 131 ms
77,332 KB
testcase_10 AC 128 ms
77,440 KB
testcase_11 AC 1,076 ms
144,784 KB
testcase_12 AC 1,053 ms
144,796 KB
testcase_13 AC 1,076 ms
144,680 KB
testcase_14 AC 1,172 ms
145,212 KB
testcase_15 AC 1,083 ms
144,444 KB
testcase_16 AC 1,102 ms
144,848 KB
testcase_17 AC 1,102 ms
145,024 KB
testcase_18 AC 1,097 ms
144,628 KB
testcase_19 AC 1,072 ms
144,916 KB
testcase_20 AC 1,044 ms
144,744 KB
testcase_21 AC 1,216 ms
154,272 KB
testcase_22 AC 880 ms
159,680 KB
testcase_23 AC 985 ms
147,132 KB
testcase_24 AC 1,499 ms
150,432 KB
testcase_25 AC 1,028 ms
146,976 KB
testcase_26 AC 1,679 ms
149,152 KB
testcase_27 AC 1,037 ms
147,228 KB
testcase_28 AC 1,062 ms
146,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class LowestCommonAncestor:
    def __init__(self, n):
        self._n = n
        self._logn = 0
        v = 1
        while v <= self._n:
            v *= 2
            self._logn += 1
        self._depth = [0] * self._n
        self._distance = [0] * self._n
        self._ancestor = [[-1] * self._n for _ in range(self._logn)]
        self._size = [1] * self._n
        self._G = [[] for i in range(self._n)]

    def add_edge(self, u, v, w=1):
        self._G[u].append((v, w))
        self._G[v].append((u, w))

    def build(self, root=0):
        stack = [root]
        while stack:
            cur = stack.pop()
            if cur >= 0:
                stack.append(~cur)
                for nex, w in self._G[cur]:
                    if self._ancestor[0][nex] != cur and self._ancestor[0][cur] != nex:
                        self._ancestor[0][nex] = cur
                        self._depth[nex] = self._depth[cur] + 1
                        self._distance[nex] = self._distance[cur] + w
                        stack.append(nex)
            else:
                cur = ~cur
                for nex, w in self._G[cur]:
                    if nex == self._ancestor[0][cur]:
                        continue
                    self._size[cur] += self._size[nex]

        for k in range(1, self._logn):
            for i in range(self._n):
                if self._ancestor[k - 1][i] == -1:
                    self._ancestor[k][i] = -1
                else:
                    self._ancestor[k][i] = self._ancestor[k - 1][self._ancestor[k - 1][i]]

    def lca(self, u, v):
        if self._depth[u] > self._depth[v]:
            u, v = v, u
        for k in range(self._logn - 1, -1, -1):
            if ((self._depth[v] - self._depth[u]) >> k) & 1:
                v = self._ancestor[k][v]
        if u == v:
            return u, -1, -1
        for k in range(self._logn - 1, -1, -1):
            if self._ancestor[k][u] != self._ancestor[k][v]:
                u = self._ancestor[k][u]
                v = self._ancestor[k][v]
        return self._ancestor[0][u], u, v

    def distance(self, u, v):
        lca, _, _ = self.lca(u, v)
        return self._distance[u] + self._distance[v] - 2 * self._distance[lca] 
    
    def query(self, s, t):
        if self.distance(s,t) % 2:
            return 0
        if self._depth[s] == self._depth[t]:
            _, ps, pt = self.lca(s, t)
            return N - self._size[ps] - self._size[pt]
        if self._depth[s] < self._depth[t]:
            s, t = t, s
        d = self.distance(s, t) // 2 - 1
        for i in range(self._logn, -1, -1):
            if (d >> i) & 1:
                s = self._ancestor[i][s]
        v = -self._size[s]
        s = self._ancestor[0][s]
        v += self._size[s]
        return v

N, Q = map(int, input().split())
T = LowestCommonAncestor(N)
for i in range(N - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    T.add_edge(a, b, 1)
    
T.build(0)
for i in range(Q):
    s, t = map(int, input().split())
    s, t = s - 1, t - 1
    print(T.query(s, t))
0