結果

問題 No.806 木を道に
ユーザー FromBooskaFromBooska
提出日時 2023-02-28 13:31:58
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,234 bytes
コンパイル時間 631 ms
コンパイル使用メモリ 86,908 KB
実行使用メモリ 95,628 KB
最終ジャッジ日時 2023-10-13 20:01:28
合計ジャッジ時間 11,062 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
71,408 KB
testcase_01 AC 94 ms
71,304 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 94 ms
71,400 KB
testcase_08 AC 94 ms
71,676 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 160 ms
84,728 KB
testcase_25 AC 179 ms
87,552 KB
testcase_26 WA -
testcase_27 TLE -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

# 次数が3のところを切っていけばいい
# 閉路はない、木だから
# 切るときは、お互いに次数が3以上のところを優先する

N = int(input())
edges = [[] for i in range(N+1)]
degree = [0]*(N+1)
for i in range(N-1):
    a, b = map(int, input().split())
    edges[a].append(b)
    edges[b].append(a)
    degree[a] += 1
    degree[b] += 1


from collections import deque
que = deque()
for i in range(1, N+1):
    if degree[i] > 2:
        que.append(i)
        
count = 0
while que:
    current = que.popleft()
    
    if degree[current] > 2:
        for nxt in edges[current]:
            if degree[nxt] > 2:
                edges[current].remove(nxt)
                edges[nxt].remove(current)
                degree[current] -= 1
                degree[nxt] -= 1
                count += 1
            if degree[current] <= 2:
                break
            
    if degree[current] > 2:
        for nxt in edges[current]:
            edges[current].remove(nxt)
            edges[nxt].remove(current)
            degree[current] -= 1
            degree[nxt] -= 1
            count += 1
            if degree[current] <= 2:
                break
    #print(count, edges)
        
print(count)  
0