結果

問題 No.994 ばらばらコイン
ユーザー YamaKasaYamaKasa
提出日時 2020-06-14 17:24:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 733 bytes
コンパイル時間 1,502 ms
コンパイル使用メモリ 171,504 KB
実行使用メモリ 9,220 KB
最終ジャッジ日時 2023-09-09 21:43:12
合計ジャッジ時間 3,350 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 66 ms
9,060 KB
testcase_03 AC 69 ms
9,052 KB
testcase_04 AC 72 ms
9,032 KB
testcase_05 AC 34 ms
6,176 KB
testcase_06 AC 68 ms
9,104 KB
testcase_07 AC 69 ms
9,220 KB
testcase_08 AC 46 ms
7,244 KB
testcase_09 AC 64 ms
9,004 KB
testcase_10 AC 48 ms
7,456 KB
testcase_11 AC 54 ms
7,872 KB
testcase_12 AC 59 ms
8,284 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    int N, K;
    cin >> N >> K;
    vector<int> adj[N];
    for (int i = 0; i < N - 1; i++) {
        int a, b;
        cin >> a >> b;
        adj[a - 1].push_back(b - 1);
        adj[b - 1].push_back(a - 1);
    }
    deque<int> q;
    q.push_back(0);
    int cnt = 0;
    bool vis[N]{};
    vis[0] = true;
    while (!q.empty()) {
        int v = q.front();
        q.pop_front();
        for (int u : adj[v]) {
            if (vis[u]) continue;
            vis[u] = true;
            cnt++;
            q.push_back(u);
        }
    }
    if (cnt >= K - 1) {
        cout << K - 1 << "\n";
    } else {
        cout << "-1\n";
    }
    return 0;
}
0