結果

問題 No.994 ばらばらコイン
ユーザー tomatoma
提出日時 2020-02-21 21:27:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 827 bytes
コンパイル時間 1,951 ms
コンパイル使用メモリ 174,204 KB
実行使用メモリ 10,368 KB
最終ジャッジ日時 2024-04-17 07:21:23
合計ジャッジ時間 3,292 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;

int main()
{
    ll N, K;
    cin >> N >> K;
    if (N < K) {
        cout << -1 << endl;
        return 0;
    }
    vector<vector<ll>> edges(N);
    rep(i, N - 1) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }

    vector<ll> d(N, 1 << 20);
    queue<ll> q;
    d[0] = 0;
    q.push(0);
    while (!q.empty()) {
        ll now = q.front(); q.pop();
        for (int next : edges[now])if (d[now] < d[next]) {
            d[next] = d[now] + 1;
            q.push(next);
        }
    }
    sort(d.begin(), d.end());
    cout << accumulate(d.begin(), d.begin() + K, 0ll) << endl;
    return 0;
}
0