結果

問題 No.994 ばらばらコイン
ユーザー ShibuyapShibuyap
提出日時 2020-02-21 21:27:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,742 bytes
コンパイル時間 1,763 ms
コンパイル使用メモリ 172,820 KB
実行使用メモリ 19,968 KB
最終ジャッジ日時 2024-04-17 07:21:54
合計ジャッジ時間 3,250 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 6 ms
13,056 KB
testcase_02 AC 44 ms
19,968 KB
testcase_03 AC 58 ms
19,328 KB
testcase_04 AC 57 ms
19,180 KB
testcase_05 AC 29 ms
16,000 KB
testcase_06 WA -
testcase_07 AC 55 ms
19,200 KB
testcase_08 AC 35 ms
17,024 KB
testcase_09 AC 50 ms
18,688 KB
testcase_10 AC 39 ms
17,024 KB
testcase_11 WA -
testcase_12 AC 46 ms
18,284 KB
testcase_13 AC 6 ms
13,184 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 5 ms
13,032 KB
testcase_17 AC 5 ms
13,056 KB
testcase_18 AC 5 ms
13,056 KB
testcase_19 AC 5 ms
13,056 KB
testcase_20 AC 5 ms
13,056 KB
testcase_21 AC 6 ms
13,184 KB
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}

#define MAX_N 200005
vector<int> pre_G[MAX_N];
vector<int> G[MAX_N];
int par_[MAX_N];
int flag_netsukigi[MAX_N];
void make_netsukigi(int r){
    queue<int> que;
    que.push(r);
    flag_netsukigi[r] = 1;
    par_[r] = -1;

    while(que.size() > 0){
        int x = que.front();
        que.pop();
        flag_netsukigi[x] = 1;
        rep(i,pre_G[x].size()){
            if(flag_netsukigi[pre_G[x][i]] == 0){
                par_[pre_G[x][i]] = x;
                G[x].push_back(pre_G[x][i]);
                que.push(pre_G[x][i]);
            }
        }
    }
}

int main() {
    int n , k;
    cin >> n >> k;

    rep(i,n-1){
        int a, b;
        scanf("%d%d", &a, &b);
        a--; b--; // 0-indexにするため.
        pre_G[a].push_back(b);
        pre_G[b].push_back(a);
    }

    make_netsukigi(0);

    if(n < k){
        cout << -1<< endl;
        return 0;
    }

    int z[n] = {};

    queue<int> que;
    que.push(0);
    while(que.size() > 0){
        int x = que.front();
        que.pop();
        rep(i,G[x].size()){
            z[G[x][i]] = z[i] + 1;
            que.push(G[x][i]);
        }
    }

    sort(z,z+n);
    ll ans = 0;

    rep(i,k){
        ans += z[i];
    }

    cout << ans << endl;
    return 0;
}
0