結果

問題 No.994 ばらばらコイン
ユーザー kekenxkekenx
提出日時 2020-04-02 21:36:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 846 bytes
コンパイル時間 1,629 ms
コンパイル使用メモリ 172,236 KB
実行使用メモリ 9,160 KB
最終ジャッジ日時 2023-09-11 00:47:13
合計ジャッジ時間 4,445 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 4 ms
5,696 KB
testcase_02 AC 65 ms
8,816 KB
testcase_03 AC 74 ms
8,996 KB
testcase_04 AC 74 ms
8,928 KB
testcase_05 AC 37 ms
7,316 KB
testcase_06 WA -
testcase_07 AC 74 ms
8,920 KB
testcase_08 AC 51 ms
7,920 KB
testcase_09 AC 68 ms
9,052 KB
testcase_10 AC 51 ms
7,928 KB
testcase_11 AC 58 ms
8,292 KB
testcase_12 AC 63 ms
8,632 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 4 ms
5,692 KB
testcase_17 AC 3 ms
5,956 KB
testcase_18 AC 4 ms
5,692 KB
testcase_19 AC 3 ms
5,760 KB
testcase_20 AC 3 ms
5,680 KB
testcase_21 AC 3 ms
5,896 KB
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 100005;
const int INF = 1e9;
vector<int> G[MAXN];

vector<int> bfs() {
  vector<int> dist(MAXN, INF);
  queue<int> q;
  q.push(0);
  dist[0] = 0;
  while (!q.empty()) {
    int cur = q.front(); q.pop();
    for (int next: G[cur]) {
      if (dist[next] == INF) {
	dist[next] = dist[cur] + 1;
	q.push(next);
      }
    }
  }
  return dist;
}

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    a--; b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  if (k < n) {
    cout << k - 1 << '\n';
  } else if (k > n) {
    puts("-1");
  } else {
    vector<int> dist = bfs();
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
      ans += dist[i];
    }
    cout << ans << '\n';
  }
  return 0;
}
0