#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 1e6;
int n, t, m;
int best[N], dep[N], dp[N];
bool flg[N];
vector<int> g[N];
void dfs(int u, int p) {
if (u == t) {
dp[u] = -dep[u];
flg[u] = 1;
return;
}
int c = -1, cnt = 0;
for (int v : g[u]) {
if (v ^ p) {
dep[v] = dep[u] + 1;
dfs(v, u);
if (flg[v]) {
flg[u] = 1;
c = v;
} else {
++cnt;
}
}
}
priority_queue<int, vector<int>, greater<int>> pq;
for (int v : g[u]) {
if (v == p) {
continue;
}
if (~c && v ^ c) {
dp[v] -= 2 * dep[u];
}
pq.push(dp[v]);
if (pq.size() > 2) {
pq.pop();
}
}
if (!pq.size()) {
dp[u] = dep[u];
return;
}
if (pq.size() == 1) {
dp[u] = ~c ? pq.top() : dep[u];
return;
}
dp[u] = pq.top() + cnt - 1;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> t >> m; --t; --m;
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v; --u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(m, m);
cout << dp[m] + dep[t];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
23896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
204 ms |
76428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
23896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
23896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |