#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, t, m; cin >> n >> t >> m; t--; m--;
vector<vector<int>> adj(n);
for (int i = 0; i < n-1; i++){
int a, b; cin >> a >> b; a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<vector<int>> children(n);
vector<int> moves_above(n);
vector<int> dp(n);
vector<int> path_to_m;
// cerr << "hi" << endl;
auto dfs_init = [&](auto self, int u, int v) -> bool{
bool m_in_subtree = false;
if (u == m) m_in_subtree = true;
for (int neighbor : adj[u]){
if (neighbor != v){
children[u].push_back(neighbor);
m_in_subtree |= self(self, neighbor, u);
}
}
if (m_in_subtree){
path_to_m.push_back(u);
}
return m_in_subtree;
};
dfs_init(dfs_init, t, -1);
// cerr << "dfs_init" << endl;
auto dfs_actual = [&](auto self, int u, int ma) -> void{
moves_above[u] = ma;
if (u != t){
ma += children[u].size() - 1;
}
vector<int> children_dp;
for (int neighbor : children[u]){
self(self, neighbor, ma);
children_dp.push_back(dp[neighbor]);
}
sort(children_dp.begin(), children_dp.end());
if (children_dp.empty()){
dp[u] = 0;
}
else if (children_dp.size() == 1){
dp[u] = 1;
}
else{
dp[u] = children_dp[children_dp.size() - 2] + children_dp.size() - 1 + 1;
}
};
dfs_actual(dfs_actual, t, 0);
// cerr << "dfs_actual" << endl;
auto ok = [&](int mid){
int free = 0; //number of restrictions can place
for (int i = 0; i < path_to_m.size() - 1; i++){
free++;
int node = path_to_m[i];
vector<int> children_dp;
for (int child : children[node]){
if (i == 0){
children_dp.push_back(dp[child] + children[node].size() - 1 + moves_above[node] + 1);
}
else{
if (child != path_to_m[i-1]){
children_dp.push_back(dp[child] + children[node].size() - 2 + moves_above[node] + 1);
}
}
}
if (children_dp.size() == 0){}
else{
sort(children_dp.begin(), children_dp.end());
int sub_from_mid = 0;
while (children_dp.size() && children_dp.back() > mid){
children_dp.pop_back(); free--; sub_from_mid++;
if (free < 0) return false;
}
mid -= sub_from_mid;
}
if (free < 0) return false;
}
if (mid < 0) return false;
return true;
};
int lo = 0, hi = 1e9;
while (lo < hi){
int mid = (lo + hi) / 2;
if (ok(mid)){
hi = mid;
}
else{
lo = mid + 1;
}
}
cout << lo << "\n";
}
Compilation message
mousetrap.cpp: In lambda function:
mousetrap.cpp:72:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 0; i < path_to_m.size() - 1; i++){
| ~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
272 ms |
102008 KB |
Output is correct |
2 |
Correct |
246 ms |
91840 KB |
Output is correct |
3 |
Correct |
779 ms |
103812 KB |
Output is correct |
4 |
Correct |
321 ms |
52564 KB |
Output is correct |
5 |
Correct |
762 ms |
103896 KB |
Output is correct |
6 |
Correct |
758 ms |
103848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
272 ms |
102008 KB |
Output is correct |
12 |
Correct |
246 ms |
91840 KB |
Output is correct |
13 |
Correct |
779 ms |
103812 KB |
Output is correct |
14 |
Correct |
321 ms |
52564 KB |
Output is correct |
15 |
Correct |
762 ms |
103896 KB |
Output is correct |
16 |
Correct |
758 ms |
103848 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
272 ms |
115528 KB |
Output is correct |
31 |
Correct |
268 ms |
115580 KB |
Output is correct |
32 |
Correct |
512 ms |
276264 KB |
Output is correct |
33 |
Correct |
373 ms |
272208 KB |
Output is correct |
34 |
Correct |
778 ms |
117048 KB |
Output is correct |
35 |
Correct |
802 ms |
116856 KB |
Output is correct |
36 |
Correct |
1052 ms |
131468 KB |
Output is correct |
37 |
Correct |
1000 ms |
131524 KB |
Output is correct |
38 |
Correct |
605 ms |
126924 KB |
Output is correct |
39 |
Correct |
611 ms |
127040 KB |
Output is correct |
40 |
Correct |
617 ms |
126880 KB |
Output is correct |