#include "bits/stdc++.h"
using namespace std;
#define int long long
const int N = 1e6 + 50;
const long long INF = 1ll << 60;
int F[N], n, t, m, ch[N], pr[N], bala[N], A[N], h[N];
vector<int> adj[N];
void dfs2(int v, int p) {
for (auto u: adj[v]) if (u != p) {
bala[u] = bala[v] + ch[v] - 1;
dfs2(u, v);
}
}
void dfs(int v, int p) {
pr[v] = p;
vector<int> tmp;
for (auto u: adj[v]) if (u != p) {
h[u] = h[v] + 1;
dfs(u, v);
tmp.push_back(F[u]);
ch[v]++;
}
sort(tmp.rbegin(), tmp.rend());
if (tmp.size() <= 1)
F[v] = tmp.size();
else
F[v] = tmp[1] + tmp.size();
}
bool is_valid() {
int s = 0;
for (int i = 0; i <= n; i++) {
s += A[i];
if (s > i + 1) {
return false;
}
}
return true;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> t >> m;
t--, m--;
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v; u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
if (m == t) {
cout << 0 << '\n';
return 0;
}
dfs(t, t);
ch[t] = 1;
dfs2(t, t);
vector<pair<int, int>> X;
int tmp_m = m;
int child = -1;
while (m != t) {
for (auto u: adj[m]) if (u != pr[m] && u != child) {
if (m == tmp_m)
X.emplace_back(F[u] + 1 + bala[u], u);
else
X.emplace_back(F[u] + 1 + bala[u] - 1, u);
}
child = m;
m = pr[m];
}
sort(X.rbegin(), X.rend());
int moves = 0;
for (auto p: X) {
int f = p.first;
int u = p.second;
assert(h[tmp_m] - h[u] + 1 >= 0);
A[h[tmp_m] - h[u] + 1]++;
if (!is_valid()) {
cout << f + moves << '\n';
return 0;
}
moves++;
}
cout << moves << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23740 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23800 KB |
Output is correct |
5 |
Correct |
13 ms |
23820 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
296 ms |
98552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23740 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23800 KB |
Output is correct |
5 |
Correct |
13 ms |
23820 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23740 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23800 KB |
Output is correct |
5 |
Correct |
13 ms |
23820 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |