#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5+10;
int n;
int A, B;
int tt[maxn];
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;
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[u] = max(tt[u], child[i]+i+1);
return tt[u];
}
int busca(void)
{
int ini = 1, fim = path.size()-1, ans = -1;
while (ini <= fim)
{
int mid = (ini+fim)>>1;
memset(tt, 0, sizeof tt);
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
{
memset(tt, 0, sizeof tt);
ans = min(solve(B, 0, path[opt]), solve(A, 0, path[opt]));
}
printf("%d\n", ans);
}
Compilation message
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:75: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:80:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
8576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
636 ms |
24652 KB |
Output is correct |
2 |
Correct |
642 ms |
25832 KB |
Output is correct |
3 |
Incorrect |
711 ms |
27324 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |