結果

問題 No.994 ばらばらコイン
ユーザー mokraimokrai
提出日時 2020-02-22 00:59:16
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 694 bytes
コンパイル時間 118 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 818,048 KB
最終ジャッジ日時 2024-04-17 12:12:49
合計ジャッジ時間 2,894 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 410 ms
25,728 KB
testcase_03 MLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 6)

N, K = list(map(int, input().split()))
ab = []
for i in range(N-1):
    a, b = list(map(int, input().split()))
    ab.append([a-1, b-1])

if N < K:
    print(-1)
    exit()

G = []
for i in range(N):
    G.append([False] * N)

for a, b in ab:
    G[a][b] = True
    G[b][a] = True

# for i in range(N):
#     print(G[i])

VISITED = [None] * N

DEPTH = [None] * N

def dfs(p, d):
    VISITED[p] = True
    DEPTH[p] = d

    # if all(VISITED):
    #     return

    for i, b in enumerate(G[p]):
        if b and not VISITED[i]:
            dfs(i, d + 1)

dfs(0, 0)

# print(':', VISITED)
# print(':', DEPTH)

DEPTH.sort()
ans = sum(DEPTH[:K])
print(ans)
0