#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N ((ll)1e6+5)
#define M ((ll)1e9+7)
#define fi first
#define se second
ll n, t, m, st[N], ft[N], ct, w[N], dp[N], par[N], h[N], d[N], l, r, md;
vector<ll> v[N];
inline bool mpr(ll x) { return st[x] <= st[m] && ft[x] >= ft[m]; }
void dfs(ll s, ll p) {
st[s] = ++ct; ll m1, m2; m1 = m2 = -1; h[s] = h[p] + 1;
for (auto x : v[s]) if (x != p) {
dfs(x, s); par[x] = s; if (!mpr(x)) {
d[s]++; if (dp[x] >= m1) swap(m1, m2), m1 = dp[x]; else if (dp[x] >= m2) m2 = dp[x];
}
}
dp[s] = d[s] + (d[s] > 1 ? m2 : 0); if (t == s) d[s] = 0;
ft[s] = ++ct;
}
void wef(ll s, ll p) {
w[s] += d[s]; for (auto x : v[s]) if (x != p) {
w[x] = w[s], dp[x] += w[s]; wef(x, s);
}
}
inline bool isv(ll x) {
ll a = m, b, o = 0; while (a != t) {
b = 0;
for (auto y : v[a])
if (!mpr(y) && dp[y] + o - b > x)
b++, o++;
if (o > h[m] - h[a] + 1 || o > x) return false;
a = par[a];
}return true;
}
int32_t main() {
#define int ll
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> t >> m; ll a, b; for (int i = 1; i < n; i++) cin >> a >> b, v[a].push_back(b), v[b].push_back(a);
dfs(t, 0);
wef(t, 0);
l = -1; r = n; while (r - l > 1) {
md = (r + l) / 2; if (isv(md)) r = md;
else l = md;
}
cout << r;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23836 KB |
Output is correct |
2 |
Correct |
13 ms |
23892 KB |
Output is correct |
3 |
Correct |
13 ms |
23820 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23824 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
15 ms |
23816 KB |
Output is correct |
10 |
Correct |
12 ms |
23816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
303 ms |
127172 KB |
Output is correct |
2 |
Correct |
276 ms |
116832 KB |
Output is correct |
3 |
Correct |
889 ms |
129164 KB |
Output is correct |
4 |
Correct |
415 ms |
76448 KB |
Output is correct |
5 |
Correct |
878 ms |
129080 KB |
Output is correct |
6 |
Correct |
862 ms |
129164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23836 KB |
Output is correct |
2 |
Correct |
13 ms |
23892 KB |
Output is correct |
3 |
Correct |
13 ms |
23820 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23824 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
15 ms |
23816 KB |
Output is correct |
10 |
Correct |
12 ms |
23816 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23940 KB |
Output is correct |
13 |
Correct |
13 ms |
23948 KB |
Output is correct |
14 |
Correct |
13 ms |
24020 KB |
Output is correct |
15 |
Correct |
13 ms |
23892 KB |
Output is correct |
16 |
Correct |
15 ms |
23840 KB |
Output is correct |
17 |
Correct |
12 ms |
23892 KB |
Output is correct |
18 |
Correct |
13 ms |
23892 KB |
Output is correct |
19 |
Correct |
12 ms |
23892 KB |
Output is correct |
20 |
Correct |
14 ms |
23952 KB |
Output is correct |
21 |
Correct |
12 ms |
23892 KB |
Output is correct |
22 |
Correct |
14 ms |
23924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23836 KB |
Output is correct |
2 |
Correct |
13 ms |
23892 KB |
Output is correct |
3 |
Correct |
13 ms |
23820 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23824 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
15 ms |
23816 KB |
Output is correct |
10 |
Correct |
12 ms |
23816 KB |
Output is correct |
11 |
Correct |
303 ms |
127172 KB |
Output is correct |
12 |
Correct |
276 ms |
116832 KB |
Output is correct |
13 |
Correct |
889 ms |
129164 KB |
Output is correct |
14 |
Correct |
415 ms |
76448 KB |
Output is correct |
15 |
Correct |
878 ms |
129080 KB |
Output is correct |
16 |
Correct |
862 ms |
129164 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23940 KB |
Output is correct |
19 |
Correct |
13 ms |
23948 KB |
Output is correct |
20 |
Correct |
13 ms |
24020 KB |
Output is correct |
21 |
Correct |
13 ms |
23892 KB |
Output is correct |
22 |
Correct |
15 ms |
23840 KB |
Output is correct |
23 |
Correct |
12 ms |
23892 KB |
Output is correct |
24 |
Correct |
13 ms |
23892 KB |
Output is correct |
25 |
Correct |
12 ms |
23892 KB |
Output is correct |
26 |
Correct |
14 ms |
23952 KB |
Output is correct |
27 |
Correct |
12 ms |
23892 KB |
Output is correct |
28 |
Correct |
14 ms |
23924 KB |
Output is correct |
29 |
Correct |
12 ms |
23780 KB |
Output is correct |
30 |
Correct |
330 ms |
127112 KB |
Output is correct |
31 |
Correct |
334 ms |
127096 KB |
Output is correct |
32 |
Correct |
439 ms |
209544 KB |
Output is correct |
33 |
Correct |
374 ms |
217252 KB |
Output is correct |
34 |
Correct |
880 ms |
129124 KB |
Output is correct |
35 |
Correct |
862 ms |
129292 KB |
Output is correct |
36 |
Correct |
909 ms |
138512 KB |
Output is correct |
37 |
Correct |
921 ms |
138576 KB |
Output is correct |
38 |
Correct |
715 ms |
136784 KB |
Output is correct |
39 |
Correct |
710 ms |
136664 KB |
Output is correct |
40 |
Correct |
745 ms |
136764 KB |
Output is correct |