結果

問題 No.763 Noelちゃんと木遊び
ユーザー titan23titan23
提出日時 2022-06-11 13:54:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 297 ms / 2,000 ms
コード長 924 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 81,812 KB
実行使用メモリ 103,496 KB
最終ジャッジ日時 2023-10-21 23:00:27
合計ジャッジ時間 5,822 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 164 ms
103,496 KB
testcase_01 AC 139 ms
82,540 KB
testcase_02 AC 254 ms
97,596 KB
testcase_03 AC 169 ms
86,316 KB
testcase_04 AC 150 ms
86,184 KB
testcase_05 AC 171 ms
87,172 KB
testcase_06 AC 296 ms
102,696 KB
testcase_07 AC 273 ms
98,816 KB
testcase_08 AC 177 ms
86,520 KB
testcase_09 AC 138 ms
82,540 KB
testcase_10 AC 97 ms
77,456 KB
testcase_11 AC 297 ms
103,112 KB
testcase_12 AC 264 ms
100,184 KB
testcase_13 AC 276 ms
98,376 KB
testcase_14 AC 229 ms
90,976 KB
testcase_15 AC 171 ms
86,736 KB
testcase_16 AC 95 ms
77,904 KB
testcase_17 AC 169 ms
86,352 KB
testcase_18 AC 296 ms
103,448 KB
testcase_19 AC 263 ms
100,340 KB
testcase_20 AC 272 ms
100,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 非再帰

import sys
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**6)
import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')

##################

n = int(input())
G = [[] for _ in range(n)]
for _ in range(n-1):
  u, v = map(int, input().split())
  u -= 1
  v -= 1
  G[u].append(v)
  G[v].append(u)

def tree(G: list, root: int) -> list:
  "get topological_sort and dist."
  n = len(G)
  todo = [root]
  dist = [-1] * n
  order = [root]
  dist[root] = 0
  while todo:
    v = todo.pop()
    d = dist[v] + 1
    for x in G[v]:
      if dist[x] != -1:
        continue
      dist[x] = d
      todo.append(x)
      order.append(x)
  return (order, dist)

dp = [[0, 1] for _ in range(n)]
order, dist = tree(G, 0)

for v in order[::-1]:
  for x in G[v]:
    if dist[x] < dist[v]:
      continue
    dp[v][0] += max(dp[x][0], dp[x][1])
    dp[v][1] += max(dp[x][0], dp[x][1]-1)

print(max(dp[0]))
0