結果

問題 No.2337 Equidistant
ユーザー yupoohyupooh
提出日時 2023-06-02 22:55:56
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,368 bytes
コンパイル時間 683 ms
コンパイル使用メモリ 11,096 KB
実行使用メモリ 135,452 KB
最終ジャッジ日時 2023-08-28 05:17:05
合計ジャッジ時間 48,623 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
8,612 KB
testcase_01 AC 18 ms
8,400 KB
testcase_02 AC 19 ms
8,552 KB
testcase_03 AC 17 ms
8,452 KB
testcase_04 AC 18 ms
8,400 KB
testcase_05 AC 17 ms
8,508 KB
testcase_06 AC 41 ms
9,676 KB
testcase_07 AC 40 ms
9,760 KB
testcase_08 AC 41 ms
9,736 KB
testcase_09 AC 41 ms
9,780 KB
testcase_10 AC 42 ms
9,676 KB
testcase_11 AC 3,941 ms
115,832 KB
testcase_12 TLE -
testcase_13 AC 3,908 ms
115,872 KB
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 AC 3,935 ms
115,844 KB
testcase_18 TLE -
testcase_19 TLE -
testcase_20 AC 3,948 ms
115,692 KB
testcase_21 TLE -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(500000)
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=[0]*n
def dfs(v, e):
  depth[v] = e
  seen[v]=1
  tot=1
  for w in adj[v]:
    if seen[w]==0:
      tot+=dfs(w, e+1)
  size[v]=tot
  return tot
dfs(0, 0)
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