結果

問題 No.1103 Directed Length Sum
ユーザー marroncastlemarroncastle
提出日時 2020-09-13 05:02:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,555 ms / 3,000 ms
コード長 710 bytes
コンパイル時間 565 ms
コンパイル使用メモリ 87,000 KB
実行使用メモリ 261,128 KB
最終ジャッジ日時 2023-08-30 23:24:48
合計ジャッジ時間 16,498 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,332 KB
testcase_01 AC 71 ms
71,440 KB
testcase_02 AC 724 ms
261,128 KB
testcase_03 AC 623 ms
258,768 KB
testcase_04 AC 898 ms
130,364 KB
testcase_05 AC 1,555 ms
166,328 KB
testcase_06 AC 568 ms
111,620 KB
testcase_07 AC 185 ms
85,428 KB
testcase_08 AC 253 ms
89,800 KB
testcase_09 AC 145 ms
82,432 KB
testcase_10 AC 313 ms
94,624 KB
testcase_11 AC 988 ms
135,052 KB
testcase_12 AC 596 ms
112,068 KB
testcase_13 AC 318 ms
95,040 KB
testcase_14 AC 133 ms
81,316 KB
testcase_15 AC 473 ms
104,544 KB
testcase_16 AC 1,093 ms
141,524 KB
testcase_17 AC 1,155 ms
144,388 KB
testcase_18 AC 320 ms
94,532 KB
testcase_19 AC 991 ms
136,440 KB
testcase_20 AC 159 ms
83,792 KB
testcase_21 AC 224 ms
88,688 KB
testcase_22 AC 813 ms
125,764 KB
testcase_23 AC 501 ms
106,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**8)
input=sys.stdin.readline

def dfs(start):
  global ans,mod
  stack = [start]
  depth = [-1]*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[u]*(depth[u]+1)//2
        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)

0