結果

問題 No.1637 Easy Tree Query
ユーザー titiatitia
提出日時 2021-08-06 21:25:33
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 635 ms / 2,000 ms
コード長 885 bytes
コンパイル時間 297 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 33,280 KB
最終ジャッジ日時 2024-09-17 03:30:57
合計ジャッジ時間 14,165 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

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):
    x,y=map(int,input().split())
    x-=1
    y-=1
    E[x].append(y)
    E[y].append(x)

N+=1 # ROOTの親の点を一つ余計に見るため、総ノード数を一つ増やしておく

ROOT=0

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

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

Children=[1]*(N+1)

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


ANS=0
for i in range(Q):
    x,y=map(int,input().split())
    x-=1
    ANS+=Children[x]*y
    print(ANS)
0