#include "bits/stdc++.h"
using namespace std;
#define int long long
const int N = 2000;
const long long INF = 1ll << 60;
int F[N], n, t, m, ch[N], pr[N], bala[N], A[N], h[N], delta[N];
vector<int> adj[N];
vector<pair<int, int>> X[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);
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[h[tmp_m] - h[u] + 1].emplace_back(F[u] + 1 + bala[u], u);
else
X[h[tmp_m] - h[u] + 1].emplace_back(F[u] + 1 + bala[u] - 1, u);
}
child = m;
m = pr[m];
}
for (int i = 0; i <= n; i++)
sort(X[i].begin(), X[i].end());
int moves = 0;
for (int i = 0; i <= n; i++) {
int z = -1;
for (int j = 0; j <= n; j++) if (X[j].size()) {
if (z == -1 || X[j].back().first + delta[j] > X[z].back().first + delta[z])
z = j;
}
if (z == -1) {
break;
}
int f = X[z].back().first + delta[z];
int u = X[z].back().second;
X[z].pop_back();
for (int j = 0; j <= z; j++)
delta[j]--;
A[h[tmp_m] - h[u] + 1]++;
if (!is_valid()) {
cout << f + moves << '\n';
return 0;
}
moves++;
}
cout << moves << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
424 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
1236 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
424 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
420 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
428 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
424 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
420 KB |
Output is correct |
11 |
Runtime error |
3 ms |
1236 KB |
Execution killed with signal 11 |
12 |
Halted |
0 ms |
0 KB |
- |