結果

問題 No.994 ばらばらコイン
ユーザー 👑 CleyL
提出日時 2020-02-21 21:32:56
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 836 bytes
コンパイル時間 890 ms
コンパイル使用メモリ 77,632 KB
実行使用メモリ 9,216 KB
最終ジャッジ日時 2024-10-08 21:16:20
合計ジャッジ時間 2,466 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct dta{
  int nm;
  int nw;

};
bool lck[100000];
int main(){
  int n,k;cin>>n>>k;k--;
  if(!k){
    cout << 0 << endl;
    return 0;
  }
  vector<int> nya[n];
  for(int i = 0; n-1 > i; i++){
    int a,b;cin>>a>>b;a--;b--;
    nya[a].push_back(b);
    nya[b].push_back(a);
  }
  queue<dta> A;
  A.push({0,0});
  lck[0] = true;
  int ans = 0;
  while(!A.empty()){
    dta Z = A.front();A.pop();
    for(int i = 0; nya[Z.nm].size() > i; i++){
      if(!lck[nya[Z.nm][i]]){
        A.push({nya[Z.nm][i],Z.nw+1});
        //cout << "!!" << Z.nm << " " << nya[Z.nm][i] << endl;
        k--;
        ans++;
        lck[nya[Z.nm][i]] = true;
        if(!k){
          cout << ans << endl;
          return 0;
        }
      }
    }
  }
  cout << -1 << endl;
  
}
0