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 maxn = 3e5+10;
int n;
int A, B;
vector<int> grafo[maxn];
vector<int> path;
bool comp(int a, int b) {return a>b;}
bool dfs(int u, int p)
{
if (u == B)
{
path.push_back(B);
return 1;
}
for (auto v: grafo[u])
{
if (v == p) continue;
if (!dfs(v, u)) continue;
path.push_back(u);
return 1;
}
return 0;
}
int solve(int u, int p, int block)
{
vector<int> child;
int tt = 0;
for (auto v: grafo[u])
if (v != p && v != block)
child.push_back(solve(v, u, block));
sort(child.begin(), child.end(), comp);
for (int i = 0; i < child.size(); i++)
tt = max(tt, child[i]+i+1);
return tt;
}
int busca(void)
{
int ini = 1, fim = path.size()-1, ans = -1;
while (ini <= fim)
{
int mid = (ini+fim)>>1;
int ans1 = solve(A, 0, path[mid]);
int ans2 = solve(B, 0, path[mid-1]);
if (ans2 >= ans1) ans = mid-1, ini = mid+1;
else fim = mid-1;
}
return ans;
}
int main(void)
{
scanf("%d %d %d", &n, &A, &B);
for (int i = 1; i < n; i++)
{
int u, v;
scanf("%d %d", &u, &v);
grafo[u].push_back(v);
grafo[v].push_back(u);
}
dfs(A, 0);
reverse(path.begin(), path.end());
int opt = busca();
int ans;
if (opt == -1)
ans = solve(A, 0, path[1]);
else
{
ans = solve(B, 0, path[opt]);
if (opt < path.size()-2)
ans = min(ans, solve(A, 0, path[opt+2]));
}
printf("%d\n", ans);
}
Compilation message (stderr)
torrent.cpp: In function 'int solve(int, int, int)':
torrent.cpp:47:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < child.size(); i++)
~~^~~~~~~~~~~~~~
torrent.cpp: In function 'int main()':
torrent.cpp:96:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (opt < path.size()-2)
~~~~^~~~~~~~~~~~~~~
torrent.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &n, &A, &B);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
torrent.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |