結果

問題 No.994 ばらばらコイン
ユーザー ooooobaoooooba
提出日時 2021-05-15 19:49:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,095 bytes
コンパイル時間 1,409 ms
コンパイル使用メモリ 127,744 KB
実行使用メモリ 8,960 KB
最終ジャッジ日時 2024-04-14 09:57:24
合計ジャッジ時間 3,086 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 70 ms
8,704 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 52 ms
7,168 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,948 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <optional>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
    int32_t n, k;
    cin >> n >> k;
    vector<vector<int32_t>> tr(n);
    for (auto i = 0; i < n - 1; ++i) {
        int32_t a, b;
        cin >> a >> b;
        --a;
        --b;
        tr[a].push_back(b);
        tr[b].push_back(a);
    }
    if (k > n) {
        cout << -1 << endl;
        return 0;
    }
    int64_t ans = 0;
    queue<pair<int32_t, int32_t>> que;
    que.push({0, 0});
    vector<bool> visited(n);
    visited[0] = true;
    while (k > 0) {
        auto p = que.front();
        que.pop();
        ans += p.second;
        --k;
        for (auto v : tr[p.first]) {
            if (visited[v])
                continue;
            que.push({v, p.second + 1});
            visited[v] = true;
        }
    }
    cout << ans << endl;
    return 0;
}
0