結果

問題 No.1103 Directed Length Sum
ユーザー marroncastlemarroncastle
提出日時 2020-09-13 05:05:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,499 ms / 3,000 ms
コード長 606 bytes
コンパイル時間 625 ms
コンパイル使用メモリ 87,012 KB
実行使用メモリ 258,912 KB
最終ジャッジ日時 2023-08-30 23:29:10
合計ジャッジ時間 15,441 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,504 KB
testcase_01 AC 68 ms
71,288 KB
testcase_02 AC 637 ms
200,180 KB
testcase_03 AC 540 ms
258,912 KB
testcase_04 AC 826 ms
130,224 KB
testcase_05 AC 1,499 ms
166,084 KB
testcase_06 AC 566 ms
111,840 KB
testcase_07 AC 168 ms
85,544 KB
testcase_08 AC 220 ms
90,288 KB
testcase_09 AC 135 ms
82,540 KB
testcase_10 AC 300 ms
94,684 KB
testcase_11 AC 914 ms
135,184 KB
testcase_12 AC 546 ms
112,000 KB
testcase_13 AC 307 ms
94,796 KB
testcase_14 AC 128 ms
81,308 KB
testcase_15 AC 435 ms
104,124 KB
testcase_16 AC 1,023 ms
141,780 KB
testcase_17 AC 1,079 ms
144,008 KB
testcase_18 AC 284 ms
94,564 KB
testcase_19 AC 966 ms
136,440 KB
testcase_20 AC 148 ms
83,328 KB
testcase_21 AC 212 ms
88,632 KB
testcase_22 AC 752 ms
125,932 KB
testcase_23 AC 457 ms
106,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=sys.stdin.readline

def dfs(start):
  global ans,mod
  stack = [start]
  depth = [-1]*N
  depth[start] = 0
  while stack:
    v = stack.pop()
    marker = 0
    for u in edge[v]:
      if depth[u]==-1: #前半
        marker = 1
        depth[u]=depth[v]+1
        ans += depth[u]*(depth[u]+1)//2
        ans %= mod
        stack.append(u)
  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)
0