結果

問題 No.994 ばらばらコイン
ユーザー sima.tetteke
提出日時 2020-02-21 21:30:43
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 933 bytes
コンパイル時間 1,967 ms
コンパイル使用メモリ 181,260 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-10-08 21:12:35
合計ジャッジ時間 3,801 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;
typedef long long int ll;
typedef pair<ll, ll > pi;  
vector<vector<pi > > G;
vector<bool > seen;
ll N, K;

void dfs(ll idx, ll from = -1){
    seen[idx] = true;

    for(ll i = 0; i < G[idx].size(); i++){
        //次の行き先は?
        ll to = G[idx][i].first;
        //探査済み
        if(seen[to] == true){
            continue;
        }
 
        dfs(to , idx);
    }
}

int main(){
    
    cin >> N >> K;
    if(N < K){
        cout << -1 << endl;
        return 0;
    }
    //初期化
    G.assign(N, vector<pi >());
    seen.assign(N, false);
 
    //グラフ入力
    for(ll i = 0; i < N - 1; i++){
        ll u, v, w;
        w = 0;
        cin >> u >> v;
        u--;
        v--;
        //双方向に貼りましょうね
        G[u].push_back(make_pair(v, w));
        G[v].push_back(make_pair(u, w));
    }

    dfs(0);

    cout << K - 1 << endl;

}
0