結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 158 ms
103,456 KB
testcase_01 AC 127 ms
82,500 KB
testcase_02 AC 230 ms
97,556 KB
testcase_03 AC 169 ms
86,284 KB
testcase_04 AC 142 ms
86,144 KB
testcase_05 AC 155 ms
87,128 KB
testcase_06 AC 278 ms
102,656 KB
testcase_07 AC 246 ms
98,788 KB
testcase_08 AC 164 ms
86,476 KB
testcase_09 AC 125 ms
82,496 KB
testcase_10 AC 92 ms
77,412 KB
testcase_11 AC 277 ms
103,068 KB
testcase_12 AC 239 ms
100,148 KB
testcase_13 AC 247 ms
98,336 KB
testcase_14 AC 196 ms
90,932 KB
testcase_15 AC 140 ms
86,696 KB
testcase_16 AC 90 ms
77,868 KB
testcase_17 AC 150 ms
86,312 KB
testcase_18 AC 285 ms
103,404 KB
testcase_19 AC 261 ms
100,296 KB
testcase_20 AC 259 ms
100,352 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