結果
問題 |
No.1103 Directed Length Sum
|
ユーザー |
![]() |
提出日時 | 2020-07-05 16:07:52 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,227 bytes |
コンパイル時間 | 325 ms |
コンパイル使用メモリ | 82,120 KB |
実行使用メモリ | 316,476 KB |
最終ジャッジ日時 | 2024-09-22 13:30:12 |
合計ジャッジ時間 | 12,526 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | WA * 3 TLE * 1 -- * 18 |
ソースコード
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)] fro=[[]for _ in range(N)] for i in range(N-1): a,b=MI() a-=1 b-=1 # a,b=i,i+1 to[a].append(b) fro[b].append(a) root=0 for i in range(N): if len(fro[i])==0: 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 while not q.empty(): v=q.get() for nv in fro[v]: ch[nv]+=ch[v]+1 if used[nv]==0: q.put(nv) used[nv]=1 ans=0 if N>=10: exit() for i in range(N): temp=((D[i])*(ch[i]+1))%mod ans=(ans+temp)%mod print(ans) main()