結果
問題 | No.1418 Sum of Sum of Subtree Size |
ユーザー |
![]() |
提出日時 | 2021-03-05 22:31:44 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 483 ms / 2,000 ms |
コード長 | 821 bytes |
コンパイル時間 | 210 ms |
コンパイル使用メモリ | 82,296 KB |
実行使用メモリ | 179,968 KB |
最終ジャッジ日時 | 2024-10-07 03:08:50 |
合計ジャッジ時間 | 11,544 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 41 |
ソースコード
from collections import Counter,defaultdict,dequefrom heapq import heappop,heappush,heapifyfrom bisect import bisect_left,bisect_rightimport sys,math,itertools,pprint,fractionssys.setrecursionlimit(10**8)mod = 10**9+7INF = float('inf')def inp(): return int(sys.stdin.readline())def inpl(): return list(map(int, sys.stdin.readline().split()))def inpl_1(): return list(map(lambda x:int(x)-1, sys.stdin.readline().split()))def err(x): print(x); exit()n = inp()g = [[] for _ in range(n)]for _ in range(n-1):a,b = inpl_1()g[b].append(a); g[a].append(b)res = 0def dfs(u,p=-1):global ressu = 0for v in g[u]:if p==v: continuenow = dfs(v,u)res += now*(n-now)su += nowup = (n-su-1)res += up*(n-up)res += nreturn su+1dfs(0)print(res)