#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1000010;
vector <int> adia[NMAX];
int cost[NMAX];
int tata[NMAX];
int nr_op;
void dfs(int nod, int t)
{
if (t)
adia[nod].erase(find(adia[nod].begin(), adia[nod].end(), t));
tata[nod] = t;
for (auto i : adia[nod])
dfs(i, nod);
int max1 = 0, max2 = 0;
for (auto i : adia[nod]) {
if (cost[i] > max1)
max2 = max1, max1 = cost[i];
else if (cost[i] > max2)
max2 = cost[i];
}
cost[nod] = adia[nod].size() + max2;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//ifstream cin("q.in");
int n, root, x;
cin >> n >> root >> x;
if (root == x) {
cout << "0\n";
return 0;
}
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adia[a].push_back(b);
adia[b].push_back(a);
}
dfs(root, 0);
vector <int> stramosi, _sp;
while (x != root) {
stramosi.push_back(x);
_sp.push_back(0);
x = tata[x];
}
_sp.push_back(0);
for (int i = stramosi.size() - 1; i >= 0; i--) {
_sp[i] = adia[stramosi[i]].size() - (i != 0);
_sp[i] += _sp[i + 1];
}
auto verif = [&](int x) -> bool {
auto sp = _sp;
int inert = 1, operatii = 0;
for (int i = 0; i < stramosi.size(); i++) {
int prv = (i ? stramosi[i - 1] : 0);
for (auto fiu : adia[stramosi[i]]) {
if (fiu == prv)
continue;
if (cost[fiu] + operatii + sp[i] >= x) {
if (!inert)
return 1;
inert--;
operatii++;
sp[i]--;
}
}
inert++;
}
return operatii >= x;
};
int ans = 0, ok = 1;
for (int p = 1; p; p = (ok ? 2 * p : p / 2)) {
if (verif(ans + p))
ans += p;
else
ok = 0;
}
cout << ans << '\n';
return 0;
}
Compilation message
mousetrap.cpp: In lambda function:
mousetrap.cpp:74:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < stramosi.size(); i++) {
~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23808 KB |
Output is correct |
2 |
Correct |
21 ms |
23808 KB |
Output is correct |
3 |
Correct |
21 ms |
23808 KB |
Output is correct |
4 |
Correct |
21 ms |
23808 KB |
Output is correct |
5 |
Correct |
21 ms |
23808 KB |
Output is correct |
6 |
Correct |
22 ms |
23928 KB |
Output is correct |
7 |
Correct |
21 ms |
23808 KB |
Output is correct |
8 |
Correct |
22 ms |
23856 KB |
Output is correct |
9 |
Correct |
22 ms |
23808 KB |
Output is correct |
10 |
Correct |
22 ms |
23808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
362 ms |
63108 KB |
Output is correct |
2 |
Correct |
362 ms |
59312 KB |
Output is correct |
3 |
Correct |
955 ms |
64420 KB |
Output is correct |
4 |
Correct |
474 ms |
44376 KB |
Output is correct |
5 |
Correct |
1000 ms |
64476 KB |
Output is correct |
6 |
Correct |
977 ms |
64504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23808 KB |
Output is correct |
2 |
Correct |
21 ms |
23808 KB |
Output is correct |
3 |
Correct |
21 ms |
23808 KB |
Output is correct |
4 |
Correct |
21 ms |
23808 KB |
Output is correct |
5 |
Correct |
21 ms |
23808 KB |
Output is correct |
6 |
Correct |
22 ms |
23928 KB |
Output is correct |
7 |
Correct |
21 ms |
23808 KB |
Output is correct |
8 |
Correct |
22 ms |
23856 KB |
Output is correct |
9 |
Correct |
22 ms |
23808 KB |
Output is correct |
10 |
Correct |
22 ms |
23808 KB |
Output is correct |
11 |
Correct |
23 ms |
23808 KB |
Output is correct |
12 |
Correct |
24 ms |
23868 KB |
Output is correct |
13 |
Correct |
24 ms |
23884 KB |
Output is correct |
14 |
Correct |
23 ms |
23936 KB |
Output is correct |
15 |
Correct |
24 ms |
24064 KB |
Output is correct |
16 |
Correct |
24 ms |
23936 KB |
Output is correct |
17 |
Correct |
25 ms |
23936 KB |
Output is correct |
18 |
Correct |
42 ms |
23928 KB |
Output is correct |
19 |
Correct |
25 ms |
23936 KB |
Output is correct |
20 |
Correct |
24 ms |
23936 KB |
Output is correct |
21 |
Correct |
22 ms |
23936 KB |
Output is correct |
22 |
Correct |
22 ms |
23808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23808 KB |
Output is correct |
2 |
Correct |
21 ms |
23808 KB |
Output is correct |
3 |
Correct |
21 ms |
23808 KB |
Output is correct |
4 |
Correct |
21 ms |
23808 KB |
Output is correct |
5 |
Correct |
21 ms |
23808 KB |
Output is correct |
6 |
Correct |
22 ms |
23928 KB |
Output is correct |
7 |
Correct |
21 ms |
23808 KB |
Output is correct |
8 |
Correct |
22 ms |
23856 KB |
Output is correct |
9 |
Correct |
22 ms |
23808 KB |
Output is correct |
10 |
Correct |
22 ms |
23808 KB |
Output is correct |
11 |
Correct |
362 ms |
63108 KB |
Output is correct |
12 |
Correct |
362 ms |
59312 KB |
Output is correct |
13 |
Correct |
955 ms |
64420 KB |
Output is correct |
14 |
Correct |
474 ms |
44376 KB |
Output is correct |
15 |
Correct |
1000 ms |
64476 KB |
Output is correct |
16 |
Correct |
977 ms |
64504 KB |
Output is correct |
17 |
Correct |
23 ms |
23808 KB |
Output is correct |
18 |
Correct |
24 ms |
23868 KB |
Output is correct |
19 |
Correct |
24 ms |
23884 KB |
Output is correct |
20 |
Correct |
23 ms |
23936 KB |
Output is correct |
21 |
Correct |
24 ms |
24064 KB |
Output is correct |
22 |
Correct |
24 ms |
23936 KB |
Output is correct |
23 |
Correct |
25 ms |
23936 KB |
Output is correct |
24 |
Correct |
42 ms |
23928 KB |
Output is correct |
25 |
Correct |
25 ms |
23936 KB |
Output is correct |
26 |
Correct |
24 ms |
23936 KB |
Output is correct |
27 |
Correct |
22 ms |
23936 KB |
Output is correct |
28 |
Correct |
22 ms |
23808 KB |
Output is correct |
29 |
Correct |
23 ms |
23872 KB |
Output is correct |
30 |
Correct |
371 ms |
76344 KB |
Output is correct |
31 |
Correct |
358 ms |
76408 KB |
Output is correct |
32 |
Correct |
408 ms |
151168 KB |
Output is correct |
33 |
Correct |
384 ms |
139128 KB |
Output is correct |
34 |
Correct |
969 ms |
77304 KB |
Output is correct |
35 |
Correct |
984 ms |
77432 KB |
Output is correct |
36 |
Correct |
1041 ms |
84888 KB |
Output is correct |
37 |
Correct |
986 ms |
84892 KB |
Output is correct |
38 |
Correct |
797 ms |
88044 KB |
Output is correct |
39 |
Correct |
899 ms |
87788 KB |
Output is correct |
40 |
Correct |
811 ms |
87916 KB |
Output is correct |