結果

問題 No.994 ばらばらコイン
ユーザー iqueue02iqueue02
提出日時 2020-02-21 21:49:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 772 bytes
コンパイル時間 1,825 ms
コンパイル使用メモリ 173,556 KB
実行使用メモリ 9,472 KB
最終ジャッジ日時 2024-04-17 07:34:33
合計ジャッジ時間 3,434 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 70 ms
8,704 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 51 ms
7,040 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 1 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 <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;

int main(){
  int n, k;
  cin >> n >> k;
  vector<vector<int>> g(n);
  rep(i, n-1) {
    int a, b;
    cin >> a >> b;
    a--; b--;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  if (n < k) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> d(n, -1);
  queue<int> que;
  que.push(0);
  d[0] = 0;
  ll res = 0;
  while (!que.empty()) {
    int u = que.front(); que.pop();
    for (auto v : g[u]) {
      if (d[v] != -1) continue;
      d[v] = d[u] + 1;
      que.push(v);
    }
  }
  sort(d.begin(), d.end());
  for (int i = 0; i < k;i++) res += d[i];
  cout << res << endl;
  return 0;
}
0