結果

問題 No.1103 Directed Length Sum
ユーザー tonnnura172tonnnura172
提出日時 2020-07-11 04:48:35
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,383 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 82,376 KB
実行使用メモリ 849,328 KB
最終ジャッジ日時 2024-04-20 10:17:20
合計ジャッジ時間 6,675 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
66,784 KB
testcase_01 AC 63 ms
66,740 KB
testcase_02 MLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd, log2
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul, add
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
tree = [[] for _ in range(N)]
din = [0]*N
for _ in range(N-1):
    A, B = MAP()
    tree[A-1].append(B-1)
    din[B-1] += 1

root = din.index(0)

down = [0]*N
up = [0]*N
def tDFS(n, par):
    up[n] += 1
    if len(tree[n]) == 0 and par != -1:
        down[n] = 1
        return 1
    tmp = 0
    for node in tree[n]:
        if node == par:
            continue
        up[node] += up[n]
        tmp += tDFS(node, n)
    down[n] = tmp+1 
    return tmp+1

tDFS(root, -1)

q = deque([root])
ans = 0
while q:
    n = q.popleft()
    for node in tree[n]:
        ans += up[n]*down[node]
        q.append(node)
print(ans)
0