結果

問題 No.763 Noelちゃんと木遊び
ユーザー titan23titan23
提出日時 2022-06-11 14:08:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 308 ms / 2,000 ms
コード長 1,337 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 81,676 KB
実行使用メモリ 103,148 KB
最終ジャッジ日時 2023-10-21 23:24:40
合計ジャッジ時間 5,830 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 147 ms
103,148 KB
testcase_01 AC 148 ms
84,780 KB
testcase_02 AC 262 ms
97,636 KB
testcase_03 AC 179 ms
87,540 KB
testcase_04 AC 158 ms
86,112 KB
testcase_05 AC 179 ms
87,916 KB
testcase_06 AC 297 ms
102,308 KB
testcase_07 AC 270 ms
100,964 KB
testcase_08 AC 164 ms
86,608 KB
testcase_09 AC 147 ms
85,652 KB
testcase_10 AC 101 ms
78,980 KB
testcase_11 AC 308 ms
102,588 KB
testcase_12 AC 242 ms
98,352 KB
testcase_13 AC 253 ms
98,424 KB
testcase_14 AC 232 ms
96,540 KB
testcase_15 AC 176 ms
87,908 KB
testcase_16 AC 96 ms
77,996 KB
testcase_17 AC 174 ms
87,704 KB
testcase_18 AC 293 ms
102,580 KB
testcase_19 AC 250 ms
97,884 KB
testcase_20 AC 258 ms
97,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 非再帰

import sys
input = lambda: sys.stdin.readline().rstrip()

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

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)

class Tree:

  def __init__(self, G: list, n: int):
    self.n = n
    self.G = G
    self.toposo = []
    self.dist = []

  def _calc_dist_toposo(self, root: int) -> None:
    todo = [root]
    self.dist = [-1] * self.n
    self.dist[root] = 0
    self.toposo = [root]
    while todo:
      v = todo.pop()
      d = self.dist[v] + 1
      for x in self.G[v]:
        if self.dist[x] != -1:
          continue
        self.dist[x] = d
        todo.append(x)
        self.toposo.append(x)

  def get_dist(self, root: int) -> list:
    "return dist."
    if self.dist:
      return self.dist
    self._calc_dist_toposo(root)
    return self.dist

  def get_toposo(self, root: int) -> list:
    "return dist."
    if self.toposo:
      return self.toposo
    self._calc_dist_toposo(root)
    return self.toposo

tree = Tree(G, n)

dp = [[0, 1] for _ in range(n)]
order = tree.get_toposo(0)
dist = tree.get_dist(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