This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
vector<int> g[300005];
int dep[300005];
int par[300005];
int chcnt[300005];
void dfs(int u, int p = 0) {
dep[u] = dep[p] + 1;
par[u] = p;
for (int v : g[u]) {
dfs(v, u);
}
}
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;
int mxdep = 0;
for (int i = 1; i <= n; i++) {
mxdep = max(mxdep, dep[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, uselist;
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]);
uselist.push_back(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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |