結果

問題 No.2337 Equidistant
ユーザー titiatitia
提出日時 2023-10-03 04:42:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,700 ms / 4,000 ms
コード長 2,831 bytes
コンパイル時間 423 ms
コンパイル使用メモリ 87,100 KB
実行使用メモリ 205,572 KB
最終ジャッジ日時 2023-10-03 04:43:12
合計ジャッジ時間 28,780 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,260 KB
testcase_01 AC 71 ms
71,360 KB
testcase_02 AC 72 ms
71,476 KB
testcase_03 AC 70 ms
71,324 KB
testcase_04 AC 70 ms
71,552 KB
testcase_05 AC 71 ms
71,368 KB
testcase_06 AC 175 ms
79,900 KB
testcase_07 AC 161 ms
79,500 KB
testcase_08 AC 158 ms
79,636 KB
testcase_09 AC 166 ms
80,176 KB
testcase_10 AC 155 ms
79,640 KB
testcase_11 AC 1,346 ms
176,432 KB
testcase_12 AC 1,301 ms
176,372 KB
testcase_13 AC 1,355 ms
176,996 KB
testcase_14 AC 1,320 ms
177,564 KB
testcase_15 AC 1,256 ms
176,652 KB
testcase_16 AC 1,317 ms
177,032 KB
testcase_17 AC 1,218 ms
176,128 KB
testcase_18 AC 1,254 ms
177,140 KB
testcase_19 AC 1,299 ms
176,996 KB
testcase_20 AC 1,291 ms
177,216 KB
testcase_21 AC 1,139 ms
181,600 KB
testcase_22 AC 1,035 ms
205,572 KB
testcase_23 AC 1,197 ms
180,224 KB
testcase_24 AC 1,455 ms
182,344 KB
testcase_25 AC 1,275 ms
180,080 KB
testcase_26 AC 1,700 ms
182,604 KB
testcase_27 AC 1,275 ms
180,052 KB
testcase_28 AC 1,257 ms
180,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

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)

ROOT=0

QUE=[ROOT]
DEPTH=[0]*N
DEPTH[ROOT]=0

Parent=[-1]*N
Parent[ROOT]=N # ROOTの親を定めておく.
Child=[[] for i in range(N)]
TOP_SORT=[] # トポロジカルソート

while QUE: # トポロジカルソートと同時に親を見つける
    x=QUE.pop()
    TOP_SORT.append(x)
    for to in E[x]:
        if Parent[to]==-1:
            Parent[to]=x
            Child[x].append(to)
            QUE.append(to)
            DEPTH[to]=DEPTH[x]+1

Children=[1]*N

for x in TOP_SORT[::-1]: #(自分を含む)子ノードの数を調べる
    if x==ROOT:
        break
    Children[Parent[x]]+=Children[x]

DOUBLING=[[-1]*N for b in range(30)]

for i in range(N):
    if i!=ROOT:
        DOUBLING[0][i]=Parent[i]

for b in range(1,30):
    for i in range(N):
        if DOUBLING[b-1][i]!=-1:
            DOUBLING[b][i]=DOUBLING[b-1][DOUBLING[b-1][i]]

# 頂点aの先祖で深さdの頂点を探す
def search_depth(a,d):
    now=a

    for b in range(30,-1,-1):
        if (DEPTH[a]-d) & (1<<b) != 0:
            now=DOUBLING[b][now]

    return now

def lca(x,y):
    if DEPTH[x]<DEPTH[y]:
        y=search_depth(y,DEPTH[x])
    elif DEPTH[x]>DEPTH[y]:
        x=search_depth(x,DEPTH[y])

    if x==y:
        return x

    for b in range(29,-1,-1):
        if DOUBLING[b][x]!=DOUBLING[b][y]:
            x=DOUBLING[b][x]
            y=DOUBLING[b][y]

    if Parent[x]==Parent[y]:
        return Parent[x]

for tests in range(Q):
    a,b=map(int,input().split())
    a-=1
    b-=1

    if DEPTH[a]>DEPTH[b]:
        a,b=b,a

    LCA=lca(a,b)

    if LCA==a:
        dis=DEPTH[b]-DEPTH[a]

        if dis%2==1:
            print(0)
            continue
        else:
            x=search_depth(b,DEPTH[b]-dis//2)
            y=search_depth(b,DEPTH[b]-dis//2+1)
            print(Children[x]-Children[y])
    else:
        dis1=DEPTH[a]-DEPTH[LCA]
        dis2=DEPTH[b]-DEPTH[LCA]

        if (dis1+dis2)%2==1:
            print(0)
            continue
        else:
            if dis1==dis2:
                x=search_depth(a,DEPTH[LCA]+1)
                y=search_depth(b,DEPTH[LCA]+1)
                print(N-Children[x]-Children[y])
            elif dis1>dis2:
                x=search_depth(a,DEPTH[a]-(dis1+dis2)//2)
                y=search_depth(a,DEPTH[a]-(dis1+dis2)//2+1)
                print(Children[x]-Children[y])
            else:
                x=search_depth(b,DEPTH[b]-(dis1+dis2)//2)
                y=search_depth(b,DEPTH[b]-(dis1+dis2)//2+1)
                print(Children[x]-Children[y])
                
                
                

    
    

        


        
        
    
    
0