//Autor: Bartłomiej Czarkowski
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
const int N = 1001000;
//~ const int N = 1010;
int n, a, b, m, q;
int par[N];
int son[N];
int cost[N];
int blk[N];
int stp[N];
int dp[N]; // ile żeby uwięzić i wyprowadzić do ojca
vector<int> v[N];
void dfs_dp(int x) {
vector<int> war = {0, 0};
for (int i : v[x]) {
if (i == par[x]) {
continue;
}
par[i] = x;
dfs_dp(i);
war.push_back(dp[i]);
}
nth_element(war.begin(), war.begin() + 1, war.end(), [&](int a, int b) {
return a > b;
});
dp[x] = war[1] + war.size() - 2;
}
bool check(int wyn) {
int x = m, nad = 1, usd = 0;
while (x != q) {
int ile = 0;
for (int i : v[x]) {
if (i != par[x] && i != son[x]) {
if (dp[i] + blk[x] + usd > wyn) {
++ile;
}
}
}
if (ile > nad) {
return false;
}
nad -= ile;
usd += ile;
x = par[x];
++nad;
}
return usd <= wyn;
}
int main() {
scanf("%d%d%d", &n, &q, &m);
for (int i = 1; i < n; ++i) {
scanf("%d%d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
++stp[a];
++stp[b];
}
dfs_dp(q);
int x = m;
while (x != q) {
son[par[x]] = x;
x = par[x];
}
while (x != m) {
x = son[x];
blk[x] = blk[par[x]];
for (int i : v[x]) {
blk[x] += (i != par[x]) && (i != son[x]);
}
}
int bp = -1, bk = n, bs;
while (bk - bp > 1) {
bs = (bp + bk) / 2;
if (check(bs)) {
bk = bs;
}
else {
bp = bs;
}
}
printf("%d\n", bk);
return 0;
}
Compilation message
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d%d%d", &n, &q, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23856 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
15 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23832 KB |
Output is correct |
7 |
Correct |
13 ms |
23828 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
15 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
373 ms |
66896 KB |
Output is correct |
2 |
Correct |
345 ms |
62468 KB |
Output is correct |
3 |
Correct |
947 ms |
67968 KB |
Output is correct |
4 |
Correct |
409 ms |
45908 KB |
Output is correct |
5 |
Correct |
989 ms |
68060 KB |
Output is correct |
6 |
Correct |
968 ms |
68104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23856 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
15 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23832 KB |
Output is correct |
7 |
Correct |
13 ms |
23828 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
15 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23840 KB |
Output is correct |
12 |
Correct |
12 ms |
23832 KB |
Output is correct |
13 |
Correct |
13 ms |
23828 KB |
Output is correct |
14 |
Correct |
16 ms |
23960 KB |
Output is correct |
15 |
Correct |
14 ms |
23948 KB |
Output is correct |
16 |
Correct |
13 ms |
23824 KB |
Output is correct |
17 |
Correct |
13 ms |
23828 KB |
Output is correct |
18 |
Correct |
13 ms |
23816 KB |
Output is correct |
19 |
Correct |
13 ms |
23828 KB |
Output is correct |
20 |
Correct |
14 ms |
23828 KB |
Output is correct |
21 |
Correct |
13 ms |
23816 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23856 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
15 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23832 KB |
Output is correct |
7 |
Correct |
13 ms |
23828 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
15 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
373 ms |
66896 KB |
Output is correct |
12 |
Correct |
345 ms |
62468 KB |
Output is correct |
13 |
Correct |
947 ms |
67968 KB |
Output is correct |
14 |
Correct |
409 ms |
45908 KB |
Output is correct |
15 |
Correct |
989 ms |
68060 KB |
Output is correct |
16 |
Correct |
968 ms |
68104 KB |
Output is correct |
17 |
Correct |
12 ms |
23840 KB |
Output is correct |
18 |
Correct |
12 ms |
23832 KB |
Output is correct |
19 |
Correct |
13 ms |
23828 KB |
Output is correct |
20 |
Correct |
16 ms |
23960 KB |
Output is correct |
21 |
Correct |
14 ms |
23948 KB |
Output is correct |
22 |
Correct |
13 ms |
23824 KB |
Output is correct |
23 |
Correct |
13 ms |
23828 KB |
Output is correct |
24 |
Correct |
13 ms |
23816 KB |
Output is correct |
25 |
Correct |
13 ms |
23828 KB |
Output is correct |
26 |
Correct |
14 ms |
23828 KB |
Output is correct |
27 |
Correct |
13 ms |
23816 KB |
Output is correct |
28 |
Correct |
12 ms |
23892 KB |
Output is correct |
29 |
Correct |
12 ms |
23820 KB |
Output is correct |
30 |
Correct |
399 ms |
80200 KB |
Output is correct |
31 |
Correct |
397 ms |
80296 KB |
Output is correct |
32 |
Correct |
551 ms |
229072 KB |
Output is correct |
33 |
Correct |
490 ms |
221164 KB |
Output is correct |
34 |
Correct |
974 ms |
81164 KB |
Output is correct |
35 |
Correct |
959 ms |
81272 KB |
Output is correct |
36 |
Correct |
985 ms |
95992 KB |
Output is correct |
37 |
Correct |
1037 ms |
95872 KB |
Output is correct |
38 |
Correct |
778 ms |
98828 KB |
Output is correct |
39 |
Correct |
766 ms |
98896 KB |
Output is correct |
40 |
Correct |
828 ms |
98832 KB |
Output is correct |