結果

問題 No.994 ばらばらコイン
ユーザー cureskolcureskol
提出日時 2020-03-19 20:07:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 643 bytes
コンパイル時間 1,648 ms
コンパイル使用メモリ 173,560 KB
実行使用メモリ 9,328 KB
最終ジャッジ日時 2023-08-20 21:05:23
合計ジャッジ時間 3,512 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 59 ms
8,796 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 39 ms
7,176 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 1 ms
4,376 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 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T>
void fin(T a){
  cout<<a<<endl;
  exit(0);
}
typedef pair<int,int> P;

signed main(){
  int n,k;cin>>n>>k;
  vector<int> edge[n];
  for(int i=0;i<n-1;i++){
    int u,v;cin>>u>>v;u--;v--;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  if(n<k)fin(-1);
  int ans=0,cnt=0;
  queue<P> que;
  que.push(P(0,0));
  vector<int> used(n,0);
  while(cnt<k){
    P p=que.front();que.pop();
    ans+=p.second*(p.second+1)/2;
    cnt++;
    used[p.first]=1;
    for(int q:edge[p.first]){
      if(used[q])continue;
      que.push(P(q,p.second+1));
    }
  }
  cout<<ans<<endl;
}
0