結果
問題 |
No.994 ばらばらコイン
|
ユーザー |
|
提出日時 | 2020-02-21 22:18:15 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,154 bytes |
コンパイル時間 | 1,153 ms |
コンパイル使用メモリ | 98,816 KB |
実行使用メモリ | 30,680 KB |
最終ジャッジ日時 | 2024-10-08 21:53:32 |
合計ジャッジ時間 | 3,002 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 7 WA * 16 |
ソースコード
#include <algorithm> #include <cmath> #include <iostream> #include <deque> #include <vector> #include <string> #include <utility> #include <queue> #include <unordered_map> #include <unordered_set> using namespace std; int main() { int n, k; cin >> n >> k; if (k > n) { cout << -1 << endl; return 0; } vector<unordered_set<int>> edges(n); for (int i = 0; i < n - 1; ++i) { int a, b; cin >> a >> b; edges.at(a - 1).emplace(b - 1); edges.at(b - 1).emplace(a - 1); } deque<int> searchingNode; for (int node : edges.at(0)) { searchingNode.emplace_back(node); } deque<int> nextCandidateNode; unordered_set<int> reachedNode; reachedNode.emplace(0); int depth = 0; long long result = 0; while (k > 1) { ++depth; for (int node : searchingNode) { --k; result += depth; if( k <= 1) { break; } for (int nextNode : edges.at(node)) { if (reachedNode.find(nextNode) == reachedNode.end()) { nextCandidateNode.emplace_back(nextNode); reachedNode.emplace(nextNode); } } } searchingNode.clear(); swap(searchingNode, nextCandidateNode); } cout << result << endl; }