結果

問題 No.994 ばらばらコイン
ユーザー plin92793134plin92793134
提出日時 2020-02-23 13:56:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,370 bytes
コンパイル時間 1,429 ms
コンパイル使用メモリ 110,188 KB
実行使用メモリ 18,688 KB
最終ジャッジ日時 2024-04-18 08:41:22
合計ジャッジ時間 2,781 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <queue>
#include <map>
#include <numeric>
#include <unordered_map>
#include <iomanip>
#include <functional>
#include <bitset>
#include <complex>
#include <stack>
#include <cstdint>



#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define rrep(i, n) for(ll i = (ll)(n-1); i >= 0; i--)
#define repi(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rrepi(i,a,b) for(ll i=(ll)(b-1);i>=(ll)(a);i--)

#define all(x) (x).begin(),(x).end()

template<class T>inline bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>inline bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

typedef long long ll;

using namespace std;


int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n,k;cin>>n>>k;
  vector<vector<ll>>ed(n);
  rep(i,n-1){
    ll a,b;cin>>a>>b;
    a--;b--;
    ed[a].push_back(b);
    ed[b].push_back(a);

  }
  vector<ll>dist(n,-1);
  dist[0]=0;
  function<void(ll ,ll)>dfs=[&](ll no,ll pa){
    for(auto nn:ed[no]){
      if(nn==pa)continue;
      dist[nn]=dist[no]+1;
      dfs(nn,no);
    }
  };
  dfs(0,-1);
  sort(all(dist));
  if(k>n){
    cout<<-1<<endl;
    return 0;
  }
  ll res=0;
  repi(i,1,k){
    res+=dist[i];
  }
  cout<<res<<endl;

  return 0;

}
0