結果

問題 No.2337 Equidistant
ユーザー yupoohyupooh
提出日時 2023-06-02 22:59:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,848 ms / 4,000 ms
コード長 2,539 bytes
コンパイル時間 363 ms
コンパイル使用メモリ 87,044 KB
実行使用メモリ 164,724 KB
最終ジャッジ日時 2023-08-28 05:24:07
合計ジャッジ時間 28,352 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
71,872 KB
testcase_01 AC 88 ms
71,784 KB
testcase_02 AC 89 ms
71,664 KB
testcase_03 AC 88 ms
71,780 KB
testcase_04 AC 87 ms
71,800 KB
testcase_05 AC 92 ms
71,736 KB
testcase_06 AC 188 ms
81,232 KB
testcase_07 AC 187 ms
81,120 KB
testcase_08 AC 181 ms
81,180 KB
testcase_09 AC 180 ms
80,932 KB
testcase_10 AC 184 ms
81,048 KB
testcase_11 AC 1,375 ms
155,220 KB
testcase_12 AC 1,208 ms
155,372 KB
testcase_13 AC 1,393 ms
155,808 KB
testcase_14 AC 1,200 ms
155,204 KB
testcase_15 AC 1,182 ms
155,484 KB
testcase_16 AC 1,201 ms
154,284 KB
testcase_17 AC 1,350 ms
157,304 KB
testcase_18 AC 1,366 ms
155,980 KB
testcase_19 AC 1,376 ms
156,748 KB
testcase_20 AC 1,208 ms
154,604 KB
testcase_21 AC 1,135 ms
162,312 KB
testcase_22 AC 826 ms
164,724 KB
testcase_23 AC 1,079 ms
158,320 KB
testcase_24 AC 1,656 ms
161,000 KB
testcase_25 AC 1,123 ms
158,976 KB
testcase_26 AC 1,848 ms
158,700 KB
testcase_27 AC 1,169 ms
159,556 KB
testcase_28 AC 1,142 ms
160,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
n,q=map(int,input().split())
adj=[[] for _ in range(n)]
S=[]
for _ in range(n-1):
  a,b=map(int,input().split())
  adj[a-1].append(b-1)
  adj[b-1].append(a-1)
  S.append((a,b))
depth = [0]*n
seen = [0]*n
size=[1]*n
pa=[-1]*n
from collections import deque
que=deque()
que.append((0,1))
seen[0]=1
while que:
  v,task=que.pop()
  if task==0:
    for w in adj[v]:
      if w!=pa[v]:
        size[v]+=size[w]
  else:
    que.append((v,0))
    seen[v]=1
    for w in adj[v]:
      if seen[w]==0:    
        que.append((w,1))
        pa[w]=v
        depth[w]=depth[v]+1
N=n
INF = 10 ** 16
depth = [INF] * N
dfs_seq = [INF] * N
# 0を根にして深さdepthとDFS順序dfs_seqを求める
todo = [(0, 0)]
count = 0
while todo:
    d, pos = todo.pop()
    if dfs_seq[pos] < INF:
        continue
    dfs_seq[pos] = count
    count += 1
    depth[pos] = d
    for next_ in adj[pos]:
        todo.append((d + 1, next_))
# 2 ** k 個上の頂点を前計算する(k < log2 N)
import math
K = int(math.log2(N)) + 1
up2 = [[INF] * N for _ in range(K)]
for a, b in S:
    if depth[a-1] < depth[b-1]:
        up2[0][b-1] = a-1
    else:
        up2[0][a-1] = b-1
for k in range(1, K):
    for p in range(N):
        if up2[k-1][p] < INF:
            up2[k][p] = up2[k-1][up2[k-1][p]]
        else:
            up2[k][p] = INF
# p個上の頂点をダブリングで求める(p <= depth[n])
def up(n, p):
    k = 0
    while n > 0:
        if n % 2:
            p = up2[k][p]
            if p == INF:
                break
        n //= 2
        k += 1
    return p
# 最小共通祖先LCAを求める
def lca(p, q):
    # 深い方のdepthを、浅い方に揃える
    if depth[p] > depth[q]:
        p, q = q, p
    d = depth[p]
    q = up(depth[q] - d, q)
    # depthをそろえた頂点が同じであれば、それが答え
    if p == q:
        return p
    # さもなければ、前計算した2**k個上の頂点を順に見比べて、答えを探索する
    k = K - 1
    while k >= 0:
        p1, q1 = up2[k][p], up2[k][q]
        if p1 != q1:
            p, q = p1, q1
        else:
            ans = p1
        k -= 1
    return ans
for _ in range(q):
  s,t=map(int,input().split())
  s-=1
  t-=1
  x=lca(s,t)
  d=depth[s]+depth[t]-2*depth[x]
  if depth[s]<depth[t]:
    s,t=t,s
  if d%2==1:
    print(0)
    continue
  if depth[s]==depth[t]:
    u1=up(d//2-1,s)
    u2=up(d//2-1,t)
    print(n-size[u1]-size[u2])
    continue
  u1=up(d//2,s)
  u2=up(d//2-1,s)
  print(size[u1]-size[u2])
0