結果

問題 No.1098 LCAs
ユーザー marroncastlemarroncastle
提出日時 2020-06-26 23:25:03
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,281 ms / 2,000 ms
コード長 544 bytes
コンパイル時間 74 ms
コンパイル使用メモリ 10,748 KB
実行使用メモリ 86,944 KB
最終ジャッジ日時 2023-09-18 08:07:58
合計ジャッジ時間 15,577 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,740 KB
testcase_01 AC 16 ms
7,832 KB
testcase_02 AC 16 ms
7,784 KB
testcase_03 AC 17 ms
7,744 KB
testcase_04 AC 17 ms
7,800 KB
testcase_05 AC 16 ms
7,788 KB
testcase_06 AC 16 ms
7,780 KB
testcase_07 AC 16 ms
7,696 KB
testcase_08 AC 16 ms
7,792 KB
testcase_09 AC 16 ms
7,744 KB
testcase_10 AC 16 ms
7,692 KB
testcase_11 AC 15 ms
7,716 KB
testcase_12 AC 16 ms
7,796 KB
testcase_13 AC 19 ms
8,104 KB
testcase_14 AC 19 ms
8,240 KB
testcase_15 AC 19 ms
8,176 KB
testcase_16 AC 19 ms
8,192 KB
testcase_17 AC 19 ms
8,172 KB
testcase_18 AC 971 ms
44,792 KB
testcase_19 AC 1,005 ms
44,796 KB
testcase_20 AC 985 ms
44,840 KB
testcase_21 AC 995 ms
44,924 KB
testcase_22 AC 1,006 ms
44,824 KB
testcase_23 AC 866 ms
39,852 KB
testcase_24 AC 860 ms
39,848 KB
testcase_25 AC 835 ms
39,296 KB
testcase_26 AC 884 ms
45,576 KB
testcase_27 AC 878 ms
45,648 KB
testcase_28 AC 1,281 ms
83,484 KB
testcase_29 AC 1,272 ms
86,944 KB
testcase_30 AC 1,260 ms
83,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**8)

def dfs(ans,edge,visited,v):
  cnt = 1
  for w in edge[v]:
    if visited[w]==False:
      visited[w]=True
      a = dfs(ans,edge,visited,w)
      ans[v] -= a*a
      cnt += a
  ans[v] += cnt**2
  return cnt

def solve():
  N = int(input())
  edge = [[] for _ in range(N)]
  visited = [False]*N
  for _ in range(N-1):
    a,b = map(int, input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)
  ans = [0]*N
  visited[0] = True
  dfs(ans,edge,visited,0)
  return ans
print(*solve(),sep='\n')
0