結果
問題 | No.1103 Directed Length Sum |
ユーザー |
![]() |
提出日時 | 2020-07-05 16:17:51 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,141 bytes |
コンパイル時間 | 171 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 257,124 KB |
最終ジャッジ日時 | 2024-09-22 13:43:36 |
合計ジャッジ時間 | 29,588 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 2 WA * 19 TLE * 1 |
ソースコード
import sys input=sys.stdin.readline def I(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) def main(): mod=10**9+7 N=I() to=[[]for _ in range(N)] P=[-1]*N for i in range(N-1): a,b=MI() a-=1 b-=1 to[a].append(b) P[b]=a root=0 for i in range(N): if P[i]==-1: root=i break import queue q=queue.Queue() q.put(root) D=[0]*N while not q.empty(): v=q.get() for nv in to[v]: D[nv]=D[v]+1 q.put(nv) ch=[0]*N#子を根とする木のサイズ used=[0]*N for i in range(N): if len(to[i])==0: q.put(i) used[i]=1 used[root]=1 while not q.empty(): v=q.get() p=P[v] ch[p]+=ch[v]+1 if used[p]==0: q.put(p) used[p]=1 ans=0 for i in range(N): temp=((D[i])*(ch[i]+1))%mod ans=(ans+temp)%mod print(ans) main()