結果

問題 No.994 ばらばらコイン
ユーザー ooooobaoooooba
提出日時 2021-05-15 19:54:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,170 bytes
コンパイル時間 1,285 ms
コンパイル使用メモリ 128,340 KB
実行使用メモリ 9,148 KB
最終ジャッジ日時 2024-04-14 10:03:50
合計ジャッジ時間 3,174 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 72 ms
8,704 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 54 ms
7,040 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
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,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,944 KB
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;
    }
    queue<pair<int32_t, int32_t>> que;
    que.push({0, 0});
    vector<bool> visited(n);
    visited[0] = true;
    --k;
    while (k > 0) {
        auto p = que.front();
        que.pop();
        for (auto v : tr[p.first]) {
            if (visited[v])
                continue;
            que.push({v, p.second + 1});
            visited[v] = true;
            --k;
        }
    }
    int64_t ans = 0;
    while (!que.empty()) {
        ans += que.front().second;
        que.pop();
    }
    cout << ans << endl;
    return 0;
}
0