結果
問題 |
No.994 ばらばらコイン
|
ユーザー |
![]() |
提出日時 | 2020-02-21 21:47:05 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 771 bytes |
コンパイル時間 | 2,013 ms |
コンパイル使用メモリ | 176,172 KB |
実行使用メモリ | 9,472 KB |
最終ジャッジ日時 | 2024-10-08 21:33:08 |
合計ジャッジ時間 | 3,415 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 7 WA * 16 |
ソースコード
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } int64_t rs64() { int64_t n; scanf("%" SCNd64, &n); return n; } int main() { int n = ri(), k = ri(); if (k > n) { puts("-1"); return 0; } std::vector<int> hen[n]; for (int i = 0; i + 1 < n; i++) { int a = ri() - 1; int b = ri() - 1; hen[a].push_back(b); hen[b].push_back(a); } std::vector<int> depth(n, -1); depth[0] = 0; std::queue<int> que; que.push(0); while (que.size()) { int i = que.front(); que.pop(); for (auto j : hen[i]) { if (depth[j] == -1) { depth[j] = depth[i] + 1; que.push(j); } } } std::sort(depth.begin(), depth.end()); int64_t res = 0; for (int i = 0; i < k; i++) res += depth[i]; printf("%" PRId64 "\n", res); return 0; }