#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<bool> vis;
vector<vector<int>> E;
int dfs(int x) {
vis[x] = true;
vector<int> v;
for (auto i : E[x]) {
if (!vis[i]) v.push_back(dfs(i));
}
sort(v.rbegin(), v.rend());
int mx = 0;
for (int i = 0; i < (int)v.size(); i++) mx = max(mx, v[i] + i + 1);
return mx;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, a, b;
cin >> n >> a >> b; a--; b--;
E.assign(n, {});
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
E[--x].push_back(--y);
E[y].push_back(x);
}
int mn = INT_MAX;
for (int i = 0; i < n; i++) {
if (i == b) continue;
vis.assign(n, false);
vis[a] = vis[b] = vis[i] = true;
int y = dfs(b);
vis[i] = false;
int x = dfs(a);
mn = min(mn, max(x, y));
}
cout << mn;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
344 KB |
Output is correct |
2 |
Incorrect |
23 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2061 ms |
20316 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |