結果

問題 No.2337 Equidistant
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2023-06-02 22:36:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,354 ms / 4,000 ms
コード長 3,018 bytes
コンパイル時間 1,127 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 173,456 KB
最終ジャッジ日時 2024-06-09 00:14:29
合計ジャッジ時間 34,528 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
54,144 KB
testcase_01 AC 45 ms
54,144 KB
testcase_02 AC 44 ms
54,272 KB
testcase_03 AC 47 ms
54,272 KB
testcase_04 AC 46 ms
54,272 KB
testcase_05 AC 43 ms
54,144 KB
testcase_06 AC 183 ms
79,104 KB
testcase_07 AC 185 ms
79,264 KB
testcase_08 AC 172 ms
79,232 KB
testcase_09 AC 174 ms
79,068 KB
testcase_10 AC 182 ms
78,848 KB
testcase_11 AC 1,638 ms
151,552 KB
testcase_12 AC 1,623 ms
142,460 KB
testcase_13 AC 1,677 ms
142,448 KB
testcase_14 AC 1,677 ms
142,724 KB
testcase_15 AC 1,673 ms
142,356 KB
testcase_16 AC 1,606 ms
142,604 KB
testcase_17 AC 1,612 ms
142,604 KB
testcase_18 AC 1,627 ms
142,664 KB
testcase_19 AC 1,641 ms
142,764 KB
testcase_20 AC 1,717 ms
142,476 KB
testcase_21 AC 1,621 ms
140,064 KB
testcase_22 AC 1,270 ms
173,456 KB
testcase_23 AC 1,552 ms
150,640 KB
testcase_24 AC 2,189 ms
140,256 KB
testcase_25 AC 1,547 ms
155,392 KB
testcase_26 AC 2,354 ms
141,352 KB
testcase_27 AC 1,649 ms
156,160 KB
testcase_28 AC 1,622 ms
151,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

## library of LCA by class
## index start from 0

import sys
sys.setrecursionlimit(10**5+5)
from collections import deque

class LCA:
    def __init__(self,n):
        self.size = n
        self.bitlen = n.bit_length()
        self.ancestor = [[0]*self.size for i in range(self.bitlen)]
        self.depth = [-1]*self.size
        self.dis = [-1]*self.size

    ## using [log_n][n] [n][log_n]
    ## [log_n][n] is tend to faster than [n][log_n]
    ## get parent by bfs is probably faster than dfs
    def make(self,root):
        self.depth[root] = 0
        self.dis[root] = 0
        q = deque([root])
        while q:
            now = q.popleft()
            for nex in e[now]:
                if self.depth[nex]>= 0:
                    continue
                self.depth[nex] = self.depth[now]+1
                self.dis[nex] = self.dis[now]+1
                self.ancestor[0][nex] = now
                q.append(nex)
        for i in range(1,self.bitlen):
            for j in range(self.size):
                if self.ancestor[i-1][j] > 0:
                    self.ancestor[i][j] = self.ancestor[i-1][self.ancestor[i-1][j]]
    
    def lca(self,x,y):
        dx = self.depth[x]
        dy = self.depth[y]
        if dx < dy:
            x,y = y,x
            dx,dy = dy,dx
        dif = dx-dy
        while dif:
            s = dif & (-dif)
            x = self.ancestor[s.bit_length()-1][x]
            dif -= s
        while x != y:
            j = 0
            while self.ancestor[j][x] != self.ancestor[j][y]:
                j += 1
            if j == 0:
                return self.ancestor[0][x]
            x = self.ancestor[j-1][x]
            y = self.ancestor[j-1][y]
        return x

    def par(self,x,dep):
        now = x
        for i in range(self.bitlen)[::-1]:
            if 1 << i <= dep:
                now = self.ancestor[i][now]
                dep -= 1<<i
        return now

n,Q = map(int,input().split())
e = [[] for i in range(n)]
for i in range(n-1):
    a,b= map(int,input().split())
    a -= 1
    b -= 1
    e[a].append(b)
    e[b].append(a)

childs = [0]*n
vis = [0]*n
q = [0]
vis[0] = 1
topo = []
par = [-1]*n
while q:
    now = q.pop()
    topo.append(now)
    for nex in e[now]:
        if vis[nex]:
            continue
        par[nex] = now
        vis[nex] = 1
        q.append(nex)

for now in topo[::-1]:
    count = 1
    for nex in e[now]:
        if nex == par[now]:
            continue
        count += childs[nex]
    childs[now] = count

lca = LCA(n)
lca.make(0)

for _ in range(Q):
    s,t = [int(x)-1 for x in input().split()]

    if lca.depth[s] < lca.depth[t]:
        s,t = t,s

    p = lca.lca(s,t)

    dis = lca.depth[s]+lca.depth[t] - lca.depth[p]*2
    if dis%2:
        print(0)
        continue

    
    center = lca.par(s,dis//2)

    if lca.depth[s] == lca.depth[t]:
        ans = n - childs[lca.par(s,dis//2-1)] - childs[lca.par(t,dis//2-1)]
    else:
        ans = childs[center]
        ans -= childs[lca.par(s,dis//2-1)]
    print(ans)
0