結果

問題 No.994 ばらばらコイン
ユーザー KKT89KKT89
提出日時 2020-02-21 21:31:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 804 bytes
コンパイル時間 1,016 ms
コンパイル使用メモリ 87,680 KB
実行使用メモリ 9,600 KB
最終ジャッジ日時 2024-04-17 07:26:08
合計ジャッジ時間 2,156 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
typedef long long int ll;
int d[100100];
vector<int> g[100100];
void dfs(int s,int p){
    for(int t:g[s]){
        if(t!=p){
            d[t]=d[s]+1;
            dfs(t,s);
        }
    }
}
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,k; cin >> n >> k;
    if(n<k){
        cout << -1 << endl;
        return 0;
    }
    for(int i=0;i<n-1;i++){
        int x,y; cin >> x >> y;
        x--; y--;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    d[0]=0;
    dfs(0,-1);
    vector<int> a(n);
    for(int i=0;i<n;i++){
        a[i]=d[i];
    }
    sort(a.begin(),a.end());
    ll ans=0;
    for(int i=0;i<k;i++){
        ans+=a[i];
    }
    cout << ans << endl;
}
0