#include <iostream>
#include <vector>
#include <algorithm>
#define pb push_back
#define all(xs) xs.begin(), xs.end()
#define rep(i, n) for (int i=0; i<(n); i++)
using namespace std;
int N, T, M;
vector<int> G[1000000];
int dp[1000000];
bool sub[1000000];
void dfs(int x, int p) {
vector<int> values;
int ctr = 0, ti = -1;
sub[x] = x == M;
for (int t : G[x]) {
if (t == p) continue;
dfs(t, x);
sub[x] |= sub[t];
if (sub[t]) ti = t;
values.pb(dp[t]);
ctr++;
}
if (ti != -1) {
dp[x] = (ctr-1) + dp[ti];
return;
}
sort(all(values), greater<int>());
if (values.size() >= 2) ctr += values[1];
dp[x] = ctr;
}
signed main() {
cin >> N >> T >> M;
T--, M--;
rep(i, N-1) {
int u, v;
cin >> u >> v;
u--, v--;
G[u].pb(v);
G[v].pb(u);
}
dfs(T, -1);
cout << dp[T] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
30336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1076 ms |
61620 KB |
Output is correct |
2 |
Correct |
969 ms |
58452 KB |
Output is correct |
3 |
Correct |
1919 ms |
62676 KB |
Output is correct |
4 |
Correct |
879 ms |
46572 KB |
Output is correct |
5 |
Correct |
1936 ms |
62676 KB |
Output is correct |
6 |
Correct |
1976 ms |
62676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
30336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
30336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |