結果

問題 No.994 ばらばらコイン
ユーザー kekenx
提出日時 2020-04-02 21:36:23
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 846 bytes
コンパイル時間 1,653 ms
コンパイル使用メモリ 173,012 KB
実行使用メモリ 9,216 KB
最終ジャッジ日時 2024-06-28 15:19:08
合計ジャッジ時間 3,842 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17 WA * 6
権限があれば一括ダウンロードができます

ソースコード

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