結果
問題 |
No.1103 Directed Length Sum
|
ユーザー |
|
提出日時 | 2020-09-13 05:16:37 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,631 ms / 3,000 ms |
コード長 | 745 bytes |
コンパイル時間 | 336 ms |
コンパイル使用メモリ | 82,196 KB |
実行使用メモリ | 261,668 KB |
最終ジャッジ日時 | 2025-01-02 23:44:50 |
合計ジャッジ時間 | 15,519 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 22 |
ソースコード
import sys input=sys.stdin.readline def dfs(start): global ans,mod stack = [start] depth = [-1]*N child_num = [0]*N depth[start] = 0 while stack: v = stack[-1] marker = 0 for u in edge[v]: if depth[u]==-1: #前半 marker = 1 depth[u]=depth[v]+1 stack.append(u) else: #後半 ans += (depth[v]+1)*(child_num[u]+1) child_num[v] += child_num[u]+1 ans %= mod if marker==0: #後半だったら stack.pop() return N = int(input()) mod = 10**9+7 edge = [[] for _ in range(N)] roots = [True]*N for i in range(N-1): a,b = list(map(int, input().split())) roots[b-1] = False edge[a-1].append(b-1) root = roots.index(True) ans = 0 dfs(root) print(ans)