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;
const int kN = 3e5;
int dep[1 + kN], dp[1 + kN];
vector<int> g[1 + kN], nodes[1 + kN];
void maxSelf(int &x, int y) {
if (x < y) {
x = y;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, a, b;
cin >> n >> a >> b;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
int maxDep = 0;
dep[a] = 1;
dep[b] = 1;
queue<int> q;
q.emplace(a);
if (a != b) {
q.emplace(b);
}
while (!q.empty()) {
int u = q.front();
q.pop();
maxDep = dep[u];
nodes[dep[u]].emplace_back(u);
for (int v : g[u]) {
if (dep[v] == 0) {
dep[v] = dep[u] + 1;
q.emplace(v);
}
}
}
for (int d = maxDep; d > 0; --d) {
for (int u : nodes[d]) {
vector<int> vals;
for (int v : g[u]) {
if (dep[v] == dep[u] + 1) {
vals.emplace_back(dp[v]);
dep[v] = 0;
}
}
sort(vals.rbegin(), vals.rend());
for (int i = 0; i < (int)vals.size(); ++i) {
maxSelf(dp[u], vals[i] + i + 1);
}
}
}
cout << max(dp[a], dp[b]) << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |