#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000005;
vector<int> adj[N];
int upCost[N];
int downCost[N];
int p[N];
vector<vector<int>> sideNodes;
int n, t, m;
bool comp(int a, int b) {
return downCost[a] < downCost[b];
}
void dfs(int node, int par) {
p[node] = par;
for (int next : adj[node]) {
if (next == par) continue;
dfs(next, node);
}
}
void calcDown(int node, int depth) {
if (adj[node].size() == 1) {
downCost[node] = depth;
return;
}
if (adj[node].size() == 2) {
downCost[node] = depth + 1;
return;
}
array<int, 2> most{0, 0};
for (int next : adj[node]) {
if (next == p[node]) continue;
calcDown(next, depth + 1);
if (downCost[next] > most[1]) most[1] = downCost[next];
if (most[0] < most[1]) swap(most[0], most[1]);
}
downCost[node] = adj[node].size() - 2 + most[1];
}
void goUp(int node, int prev = -1) {
if (node == t) return;
goUp(p[node], node);
upCost[node] += upCost[p[node]];
if (p[node] != t) {
upCost[node] += adj[p[node]].size();
upCost[node] -= 2;
}
vector<int> curSideNodes;
for (int next : adj[node]) {
if (next == prev || next == p[node]) continue;
curSideNodes.push_back(next);
upCost[next] = upCost[node];
calcDown(next, 1);
}
sort(curSideNodes.rbegin(), curSideNodes.rend(), comp);
sideNodes.push_back(curSideNodes);
}
bool poss(int most) {
int already = 0;
int canHave = 1;
for (auto it = sideNodes.rbegin(); it != sideNodes.rend(); it++) {
vector<int>& nodesVector = *it;
int blocked = 0;
for (int i : nodesVector) {
if (already + downCost[i] + upCost[i] + nodesVector.size() - blocked - 1 > most) {
if (already < canHave) {
already++;
blocked++;
} else return false;
}
}
canHave++;
}
return already <= most;
}
int bs() {
int a = 0;
int b = 2 * n;
int res = -1;
while (a <= b) {
int mid =(a +b ) / 2;
//cout << a<< " " << b << " "<< mid << " " << poss(mid) << "\n";
if (poss(mid)) {
res = mid;
b = mid - 1;
} else a = mid + 1;
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> t >> m;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(t, -1);
goUp(m);
/*
for (int i = 1; i <= n; i++ )cout << upCost[i] << " ";
cout << "\n";
for (int i = 1; i <= n; i++ )cout << downCost[i] << " ";
cout << "\n"; */
cout << bs() << "\n";
}
/*
10 1 4
1 2
2 3
2 4
3 9
3 5
4 7
4 6
6 8
7 10
*/
Compilation message
mousetrap.cpp: In function 'bool poss(int)':
mousetrap.cpp:76:86: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
76 | if (already + downCost[i] + upCost[i] + nodesVector.size() - blocked - 1 > most) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23700 KB |
Output is correct |
3 |
Correct |
14 ms |
23804 KB |
Output is correct |
4 |
Correct |
14 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23756 KB |
Output is correct |
6 |
Correct |
15 ms |
23756 KB |
Output is correct |
7 |
Correct |
15 ms |
23716 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
14 ms |
23800 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
357 ms |
62980 KB |
Output is correct |
2 |
Correct |
325 ms |
59020 KB |
Output is correct |
3 |
Correct |
843 ms |
64000 KB |
Output is correct |
4 |
Correct |
411 ms |
44048 KB |
Output is correct |
5 |
Correct |
867 ms |
64000 KB |
Output is correct |
6 |
Correct |
859 ms |
63952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23700 KB |
Output is correct |
3 |
Correct |
14 ms |
23804 KB |
Output is correct |
4 |
Correct |
14 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23756 KB |
Output is correct |
6 |
Correct |
15 ms |
23756 KB |
Output is correct |
7 |
Correct |
15 ms |
23716 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
14 ms |
23800 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
11 |
Correct |
15 ms |
23744 KB |
Output is correct |
12 |
Correct |
14 ms |
23804 KB |
Output is correct |
13 |
Correct |
14 ms |
23756 KB |
Output is correct |
14 |
Correct |
16 ms |
24060 KB |
Output is correct |
15 |
Correct |
14 ms |
23932 KB |
Output is correct |
16 |
Correct |
15 ms |
23756 KB |
Output is correct |
17 |
Correct |
14 ms |
23756 KB |
Output is correct |
18 |
Correct |
15 ms |
23840 KB |
Output is correct |
19 |
Correct |
15 ms |
23888 KB |
Output is correct |
20 |
Correct |
14 ms |
23800 KB |
Output is correct |
21 |
Correct |
14 ms |
23884 KB |
Output is correct |
22 |
Correct |
15 ms |
23856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23700 KB |
Output is correct |
3 |
Correct |
14 ms |
23804 KB |
Output is correct |
4 |
Correct |
14 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23756 KB |
Output is correct |
6 |
Correct |
15 ms |
23756 KB |
Output is correct |
7 |
Correct |
15 ms |
23716 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
14 ms |
23800 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
11 |
Correct |
357 ms |
62980 KB |
Output is correct |
12 |
Correct |
325 ms |
59020 KB |
Output is correct |
13 |
Correct |
843 ms |
64000 KB |
Output is correct |
14 |
Correct |
411 ms |
44048 KB |
Output is correct |
15 |
Correct |
867 ms |
64000 KB |
Output is correct |
16 |
Correct |
859 ms |
63952 KB |
Output is correct |
17 |
Correct |
15 ms |
23744 KB |
Output is correct |
18 |
Correct |
14 ms |
23804 KB |
Output is correct |
19 |
Correct |
14 ms |
23756 KB |
Output is correct |
20 |
Correct |
16 ms |
24060 KB |
Output is correct |
21 |
Correct |
14 ms |
23932 KB |
Output is correct |
22 |
Correct |
15 ms |
23756 KB |
Output is correct |
23 |
Correct |
14 ms |
23756 KB |
Output is correct |
24 |
Correct |
15 ms |
23840 KB |
Output is correct |
25 |
Correct |
15 ms |
23888 KB |
Output is correct |
26 |
Correct |
14 ms |
23800 KB |
Output is correct |
27 |
Correct |
14 ms |
23884 KB |
Output is correct |
28 |
Correct |
15 ms |
23856 KB |
Output is correct |
29 |
Correct |
14 ms |
23724 KB |
Output is correct |
30 |
Correct |
362 ms |
76204 KB |
Output is correct |
31 |
Correct |
356 ms |
76256 KB |
Output is correct |
32 |
Correct |
543 ms |
272200 KB |
Output is correct |
33 |
Correct |
408 ms |
96836 KB |
Output is correct |
34 |
Correct |
861 ms |
77428 KB |
Output is correct |
35 |
Correct |
866 ms |
77416 KB |
Output is correct |
36 |
Correct |
914 ms |
103100 KB |
Output is correct |
37 |
Correct |
935 ms |
103032 KB |
Output is correct |
38 |
Correct |
684 ms |
101940 KB |
Output is correct |
39 |
Correct |
749 ms |
101864 KB |
Output is correct |
40 |
Correct |
758 ms |
101944 KB |
Output is correct |