#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000005;
vector<int> adj[N];
int cost[N];
void dfs(int node, int par, int height) {
array<int, 2> most={0, 0};
for (int next : adj[node]) {
if (next == par) continue;
dfs(next, node, height + 1);
if (cost[next] > most[1]) most[1] = cost[next];
if (most[1] > most[0]) swap(most[1], most[0]);
}
if (par != -1 && adj[node].size() == 1) {
cost[node] = height;
} else if (par != -1 && adj[node].size() == 2) {
cost[node] = height + 1;
} else {
cost[node] = adj[node].size() - 2 + most[1];
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, t, m;
cin >> n >> t >> m;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(t, -1, 0);
cout << cost[m] << "\n";
}
/*
10 1 2
1 2
2 3
2 4
3 9
3 5
4 7
4 6
6 8
7 10
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
23756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
337 ms |
58936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
23756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
23756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |