#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
void Hollwo_Pelw();
signed main(){
#ifndef hollwo_pelw_local
if (fopen("mousetrap.inp", "r"))
assert(freopen("mousetrap.inp", "r", stdin)), assert(freopen("mousetrap.out", "w", stdout));
#else
auto start = chrono::steady_clock::now();
#endif
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = chrono::steady_clock::now();
cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
const int N = 1e6 + 5;
int n, s, t, deg[N], dp[N], par[N], in_path[N];
vector<int> adj[N];
void find_path(int u, int p) {
par[u] = p;
for (int v : adj[u])
if (v != p) find_path(v, u);
}
void calc_dp(int u, int p) {
int m1 = 0, m2 = 0;
for (int v : adj[u]) if (v != p) {
deg[u] ++;
calc_dp(v, u);
int f = dp[v];
if (f > m1) swap(f, m1);
if (f > m2) swap(f, m2);
}
dp[u] = deg[u] + m2;
}
bool check(int mid) {
int tot = 0;
for (int u = s; par[u]; u = par[u])
tot += deg[u];
if (tot > mid) return 0;
for (int u = s, cnt = 0, cur = 1; par[u]; u = par[u], cur ++) {
int ign = 0;
for (int v : adj[u]) if (!in_path[v]) {
ign += tot + dp[v] <= mid;
cnt += tot + dp[v] > mid;
}
tot -= ign;
if (cnt > cur) return 0;
}
return 1;
}
void Hollwo_Pelw(){
cin >> n >> t >> s;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
find_path(t, 0);
for (int u = s; u; u = par[u])
in_path[u] = 1;
for (int u = s; par[u]; u = par[u]) {
for (int v : adj[u]) if (!in_path[v]) {
deg[u] ++;
calc_dp(v, u);
}
}
int l = 0, r = 2 * n;
while (l < r) {
int m = (l + r) >> 1;
if (check(m))
r = m;
else
l = m + 1;
}
cout << l;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
15 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23788 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
15 ms |
23832 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
289 ms |
73580 KB |
Output is correct |
2 |
Correct |
263 ms |
69468 KB |
Output is correct |
3 |
Correct |
756 ms |
76808 KB |
Output is correct |
4 |
Correct |
356 ms |
52372 KB |
Output is correct |
5 |
Correct |
763 ms |
76768 KB |
Output is correct |
6 |
Correct |
755 ms |
76808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
15 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23788 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
15 ms |
23832 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23812 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
12 ms |
23892 KB |
Output is correct |
13 |
Correct |
12 ms |
23856 KB |
Output is correct |
14 |
Correct |
13 ms |
23828 KB |
Output is correct |
15 |
Correct |
14 ms |
23768 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
13 ms |
23844 KB |
Output is correct |
18 |
Correct |
13 ms |
23820 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
15 ms |
23816 KB |
Output is correct |
21 |
Correct |
12 ms |
23764 KB |
Output is correct |
22 |
Correct |
12 ms |
23788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
15 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23788 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
15 ms |
23832 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23812 KB |
Output is correct |
11 |
Correct |
289 ms |
73580 KB |
Output is correct |
12 |
Correct |
263 ms |
69468 KB |
Output is correct |
13 |
Correct |
756 ms |
76808 KB |
Output is correct |
14 |
Correct |
356 ms |
52372 KB |
Output is correct |
15 |
Correct |
763 ms |
76768 KB |
Output is correct |
16 |
Correct |
755 ms |
76808 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23892 KB |
Output is correct |
19 |
Correct |
12 ms |
23856 KB |
Output is correct |
20 |
Correct |
13 ms |
23828 KB |
Output is correct |
21 |
Correct |
14 ms |
23768 KB |
Output is correct |
22 |
Correct |
13 ms |
23764 KB |
Output is correct |
23 |
Correct |
13 ms |
23844 KB |
Output is correct |
24 |
Correct |
13 ms |
23820 KB |
Output is correct |
25 |
Correct |
12 ms |
23764 KB |
Output is correct |
26 |
Correct |
15 ms |
23816 KB |
Output is correct |
27 |
Correct |
12 ms |
23764 KB |
Output is correct |
28 |
Correct |
12 ms |
23788 KB |
Output is correct |
29 |
Correct |
12 ms |
23812 KB |
Output is correct |
30 |
Correct |
293 ms |
73676 KB |
Output is correct |
31 |
Correct |
274 ms |
73704 KB |
Output is correct |
32 |
Correct |
393 ms |
94416 KB |
Output is correct |
33 |
Correct |
316 ms |
153900 KB |
Output is correct |
34 |
Correct |
781 ms |
76704 KB |
Output is correct |
35 |
Correct |
766 ms |
76696 KB |
Output is correct |
36 |
Correct |
795 ms |
79004 KB |
Output is correct |
37 |
Correct |
789 ms |
78892 KB |
Output is correct |
38 |
Correct |
552 ms |
78424 KB |
Output is correct |
39 |
Correct |
557 ms |
78572 KB |
Output is correct |
40 |
Correct |
551 ms |
78680 KB |
Output is correct |