#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; }