結果
| 問題 | 
                            No.994 ばらばらコイン
                             | 
                    
| コンテスト | |
| ユーザー | 
                             cureskol
                         | 
                    
| 提出日時 | 2020-03-19 20:07:05 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 643 bytes | 
| コンパイル時間 | 1,722 ms | 
| コンパイル使用メモリ | 176,040 KB | 
| 実行使用メモリ | 9,472 KB | 
| 最終ジャッジ日時 | 2024-12-14 03:27:43 | 
| 合計ジャッジ時間 | 3,541 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 7 WA * 16 | 
ソースコード
#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;
}
            
            
            
        
            
cureskol