#include <bits/stdc++.h>
using namespace std;
vector<int> g[300005];
int dep[300005];
int par[300005];
int chcnt[300005];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, a, b;
cin >> n >> a >> b;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
memset(dep, 0x3f, sizeof(dep));
dep[a] = dep[b] = 0;
par[a] = par[b] = 0;
queue<int> q;
q.push(a);
q.push(b);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : g[u]) {
if (dep[v] > dep[u] + 1) {
dep[v] = dep[u] + 1;
par[v] = u;
q.push(v);
}
}
}
priority_queue<pair<int, int>> pq;
for (int i = 1; i <= n; i++) {
chcnt[par[i]]++;
}
for (int i = 1; i <= n; i++) {
if (chcnt[i] == 0)
pq.push({dep[i], i});
}
int rnd = 0;
while (!pq.empty()) {
rnd++;
vector<int> waitlist;
set<int> parf;
while (!pq.empty()) {
auto p = pq.top();
pq.pop();
if (parf.count(par[p.second])) {
waitlist.push_back(p.second);
} else {
parf.insert(par[p.second]);
chcnt[par[p.second]]--;
}
}
for (int u : parf) {
if (chcnt[u] == 0 && u != 0 && u != a && u != b)
pq.push({dep[u], u});
}
for (int u : waitlist)
pq.push({dep[u], u});
}
cout << rnd << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
10840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2080 ms |
25608 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |