#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 - 1;
} else if (par != -1 && adj[node].size() == 2) {
cost[node] = height;
} 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 |
Correct |
13 ms |
23796 KB |
Output is correct |
2 |
Correct |
13 ms |
23684 KB |
Output is correct |
3 |
Incorrect |
13 ms |
23816 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
344 ms |
58992 KB |
Output is correct |
2 |
Correct |
315 ms |
67444 KB |
Output is correct |
3 |
Correct |
803 ms |
73364 KB |
Output is correct |
4 |
Correct |
379 ms |
48476 KB |
Output is correct |
5 |
Correct |
777 ms |
73280 KB |
Output is correct |
6 |
Correct |
779 ms |
73284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23796 KB |
Output is correct |
2 |
Correct |
13 ms |
23684 KB |
Output is correct |
3 |
Incorrect |
13 ms |
23816 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23796 KB |
Output is correct |
2 |
Correct |
13 ms |
23684 KB |
Output is correct |
3 |
Incorrect |
13 ms |
23816 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |