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 mxn = 300 * 1000 + 3;
vector<int> g[mxn];
int par[mxn];
int n, a, b;
void pars(int v = a, int p = -1) {
for(int u : g[v]) if(u != p) {
par[u] = v;
pars(u, v);
}
}
int dfs(int v, int p, int cut) {
int ans = 0;
vector<int> vals;
for(int u : g[v]) if(u != p && u != cut)
vals.push_back(dfs(u, v, cut));
sort(vals.begin(), vals.end(), greater<>());
for(int i = 0; i < (int) vals.size(); i++)
ans = max(ans, vals[i] + i + 1);
return ans;
}
signed main() {
std::ios::sync_with_stdio(0);
std::cout.tie(0);
std::cin.tie(0);
cin >> n >> a >> b;
for(int u, v, i = 1; i < n; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
pars();
vector<int> path;
int z = b;
while(z)
path.push_back(z), z = par[z];
reverse(path.begin(), path.end());
// for(int o : path)
// cout << o << ' ';
// cout << endl;
int mn = 0, mx = (int) path.size() - 1, last = 0;
int ans = 2e9;
while(mn <= mx) {
int mid = (mn + mx) >> 1;
int x = dfs(a, -1, par[path[mid]]);
int y = dfs(b, -1, path[mid]);
ans = min(ans, max(x, y));
//cout << mid << ' ' << x << ' ' << y << endl;
if(x <= y)
mn = mid + 1, last = mid;
else
mx = mid - 1;
}
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
torrent.cpp: In function 'int main()':
torrent.cpp:46:45: warning: variable 'last' set but not used [-Wunused-but-set-variable]
46 | int mn = 0, mx = (int) path.size() - 1, last = 0;
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |