結果

問題 No.806 木を道に
ユーザー tobusakanatobusakana
提出日時 2022-11-27 15:49:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 230 ms / 2,000 ms
コード長 754 bytes
コンパイル時間 151 ms
コンパイル使用メモリ 82,324 KB
実行使用メモリ 97,332 KB
最終ジャッジ日時 2024-04-14 23:27:51
合計ジャッジ時間 5,087 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,332 KB
testcase_01 AC 39 ms
52,484 KB
testcase_02 AC 40 ms
53,044 KB
testcase_03 AC 38 ms
53,424 KB
testcase_04 AC 38 ms
53,344 KB
testcase_05 AC 39 ms
52,960 KB
testcase_06 AC 39 ms
52,948 KB
testcase_07 AC 39 ms
52,336 KB
testcase_08 AC 42 ms
52,764 KB
testcase_09 AC 40 ms
52,604 KB
testcase_10 AC 106 ms
78,448 KB
testcase_11 AC 104 ms
78,176 KB
testcase_12 AC 205 ms
86,936 KB
testcase_13 AC 169 ms
84,004 KB
testcase_14 AC 192 ms
85,652 KB
testcase_15 AC 204 ms
86,872 KB
testcase_16 AC 128 ms
80,464 KB
testcase_17 AC 187 ms
85,188 KB
testcase_18 AC 96 ms
77,356 KB
testcase_19 AC 108 ms
78,720 KB
testcase_20 AC 185 ms
85,548 KB
testcase_21 AC 134 ms
81,528 KB
testcase_22 AC 230 ms
88,240 KB
testcase_23 AC 223 ms
88,360 KB
testcase_24 AC 113 ms
81,592 KB
testcase_25 AC 132 ms
85,236 KB
testcase_26 AC 95 ms
80,744 KB
testcase_27 AC 163 ms
97,332 KB
testcase_28 AC 71 ms
72,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 次数1の頂点から探索を開始し、分岐が見つかった場合、分岐数 - 1を答えに加算
# 分岐があれば、それを開始地点にくっつけるイメージ

N = int(input())
G = [[] for i in range(N)]
indegree = [0] * N
for _ in range(N - 1):
    a,b = map(int,input().split())
    G[a - 1].append(b - 1)
    G[b - 1].append(a - 1)
    indegree[a - 1] += 1
    indegree[b - 1] += 1
    
for i in range(N):
    if indegree[i] == 1:
        start = i
        break
    
stack = []
stack.append([start, -1])
ans = 0
while stack:
    v,p = stack.pop()
    routes = 0
    for child in G[v]:
        if child == p:
            continue
        routes += 1
        stack.append([child, v])
    ans += max(0, routes - 1)

print(ans)
0