結果

問題 No.1103 Directed Length Sum
ユーザー marroncastlemarroncastle
提出日時 2020-12-31 18:51:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,446 ms / 3,000 ms
コード長 1,190 bytes
コンパイル時間 370 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 219,320 KB
最終ジャッジ日時 2024-04-18 00:16:43
合計ジャッジ時間 14,978 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
54,016 KB
testcase_01 AC 40 ms
54,016 KB
testcase_02 AC 533 ms
207,232 KB
testcase_03 AC 438 ms
219,320 KB
testcase_04 AC 761 ms
133,376 KB
testcase_05 AC 1,446 ms
172,288 KB
testcase_06 AC 478 ms
113,664 KB
testcase_07 AC 132 ms
84,608 KB
testcase_08 AC 181 ms
89,984 KB
testcase_09 AC 109 ms
81,920 KB
testcase_10 AC 239 ms
95,312 KB
testcase_11 AC 843 ms
138,752 KB
testcase_12 AC 466 ms
114,048 KB
testcase_13 AC 261 ms
95,616 KB
testcase_14 AC 98 ms
79,744 KB
testcase_15 AC 404 ms
106,048 KB
testcase_16 AC 982 ms
145,820 KB
testcase_17 AC 1,057 ms
148,732 KB
testcase_18 AC 253 ms
95,040 KB
testcase_19 AC 859 ms
140,196 KB
testcase_20 AC 116 ms
83,584 KB
testcase_21 AC 167 ms
88,920 KB
testcase_22 AC 698 ms
128,640 KB
testcase_23 AC 410 ms
107,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import deque
MOD = 10**9+7
class Tree:
  def __init__(self, N):
    self.V = N
    self.edge = [[] for _ in range(N)]
    self.to = [False]*N
  
  def add_edges(self, ind=1, bi=True):
    for i in range(self.V-1):
      a,b = map(int, input().split())
      a -= ind; b -= ind
      self.edge[a].append(b)
      self.to[b] = True
      if bi:
        self.edge[b].append(a)

  def add_edge(self, a, b, bi=True):
    self.edge[a].append(b)
    if bi:
      self.edge[b].append(a)

  def dfs(self):
    for v in range(self.V):
      if not self.to[v]:
        start = v
        break
    stack = deque([start])
    self.parent = [N]*N
    self.parent[start] = -1
    cnt = 0
    self.depth = [-1]*self.V
    self.depth[start] = 0
    #記録したい値の配列を定義
    while stack:
      v = stack.pop()
      for u in self.edge[v]:
        if u==self.parent[v]:
          continue
        self.parent[u]=v
        stack.append(u)
        self.depth[u] = self.depth[v]+1
        cnt += self.depth[u]*(self.depth[u]+1)//2
        cnt %= MOD
    return cnt
  
N = int(input())
G = Tree(N)
G.add_edges(ind=1, bi=False)
print(G.dfs())
0