#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;
int co = 0;
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];
assert(co++ <= n);
}
sort(X.rbegin(), X.rend());
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 << '\n';
return 0;
}
}
cout << 0 << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
322 ms |
111488 KB |
Output is correct |
2 |
Correct |
297 ms |
102728 KB |
Output is correct |
3 |
Correct |
911 ms |
113572 KB |
Output is correct |
4 |
Correct |
433 ms |
68664 KB |
Output is correct |
5 |
Correct |
918 ms |
113560 KB |
Output is correct |
6 |
Correct |
900 ms |
113832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |