#include <bits/stdc++.h>
using namespace std;
int n, a, b, x, y;
vector<int> edge[300001], dis(300001, 0);
stack<int> stk;
vector<int> instk(300001, false);
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> a >> b;
for (int i = 1; i < n; ++i) {
cin >> x >> y;
edge[x].push_back(y);
edge[y].push_back(x);
}
queue<int> q;
q.push(a), q.push(b);
stk.push(a), stk.push(b);
instk[a] = instk[b] = true;
while (!q.empty()) {
int u = q.front(); q.pop();
for (auto v : edge[u])
if (!instk[v]) stk.push(v), instk[v] = true, q.push(v);
}
while (!stk.empty()) {
int u = stk.top(); stk.pop();
instk[u] = false;
vector<int> vec;
for (auto v : edge[u])
if (!instk[v] && v != a && v != b) vec.push_back(dis[v]);
sort(vec.rbegin(), vec.rend());
for (int i = 0; i < vec.size(); ++i)
dis[u] = max(dis[u], vec[i] + i + 1);
}
cout << max(dis[a], dis[b]);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |