結果
問題 | No.994 ばらばらコイン |
ユーザー | LCA_Trim |
提出日時 | 2020-02-21 22:24:20 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 159 ms / 2,000 ms |
コード長 | 1,147 bytes |
コンパイル時間 | 1,293 ms |
コンパイル使用メモリ | 99,036 KB |
実行使用メモリ | 30,676 KB |
最終ジャッジ日時 | 2024-10-08 21:56:38 |
合計ジャッジ時間 | 3,425 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 23 |
ソースコード
#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; 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; }