#include <bits/stdc++.h>
using namespace std;
vector<int> sc[1000006];
bool isS[1000006];
int n, m, k;
int solve(int w, int f, int a = 1)
{
priority_queue<int> k;
if (w == m)
return 0;
for (auto i : sc[w])
{
if (i == f || isS[i])
continue;
k.push(solve(i, w));
}
while (k.size() < a + 1)
k.push(0);
for (int i = 0; (i < a) && (k.size() > 1); i++)
k.pop();
return sc[w].size() - 1 + k.top();
}
stack<pair<int, int>> s;
int nw;
vector<vector<int>> v;
vector<int> sp;
int dfs(int w, int f = 0, int g = 1, int z = 0)
{
if (w == m)
{
v.push_back(sp);
sp.push_back(0);
return 1;
}
for (auto i : sc[w])
{
if (i == f)
continue;
int x = dfs(i, w, g + 1, z + sc[w].size() - 2 + (f == 0));
if (x == 0)
continue;
isS[i] = 1;
vector<int> tv;
for (auto j : sc[w])
{
if (j == f || i == j)
continue;
tv.push_back(solve(j, w) + 1);
}
tv.push_back(0);
sort(tv.begin(), tv.end());
reverse(tv.begin(), tv.end());
int z = sp.back() + tv.size() - 1;
sp.push_back(z);
v.push_back(tv);
// cout << w << ' ' << sp.back() << '\n';
// for (auto j : tv)
// cout << j << ' ';
// cout << '\n';
return 1;
}
return 0;
}
map<pair<int, int>, int> mp;
int solve2(int w, int a)
{
if (w == 0 || a == 0)
return 0;
int mnw = 100000000;
if (mp[{w, a}] != 0)
return mp[{w, a}];
// cout << w << '\n';
for (int i = 0; i < min(a + 1, (int)v[w].size()); i++)
{
mnw = min(max(v[w][i] + sp[w - 1] + max((int)(v[w].size() - i - 2), 0), solve2(w - 1, a - i + 1)) + i, mnw);
}
mp[{w, a}] = mnw;
// cout << w << ' ' << a << ' ' << mnw << '\n';
return mnw;
}
int main()
{
ios_base::sync_with_stdio(0);
cin >> n >> m >> k;
for (int i = 1; i < n; i++)
{
int a, b;
cin >> a >> b;
sc[a].push_back(b);
sc[b].push_back(a);
}
dfs(k);
// cout << nw - 1 << '\n';
// s.push({0, 0});
cout << solve2(sp.size() - 1, 1);
}
Compilation message
mousetrap.cpp: In function 'int solve(int, int, int)':
mousetrap.cpp:20:21: warning: comparison of integer expressions of different signedness: 'std::priority_queue<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
20 | while (k.size() < a + 1)
| ~~~~~~~~~^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23796 KB |
Output is correct |
3 |
Correct |
14 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23892 KB |
Output is correct |
5 |
Correct |
12 ms |
23812 KB |
Output is correct |
6 |
Correct |
12 ms |
23812 KB |
Output is correct |
7 |
Correct |
12 ms |
23748 KB |
Output is correct |
8 |
Correct |
13 ms |
23812 KB |
Output is correct |
9 |
Correct |
14 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
331 ms |
55084 KB |
Output is correct |
2 |
Correct |
303 ms |
51924 KB |
Output is correct |
3 |
Correct |
751 ms |
56132 KB |
Output is correct |
4 |
Correct |
347 ms |
39996 KB |
Output is correct |
5 |
Correct |
764 ms |
56200 KB |
Output is correct |
6 |
Correct |
781 ms |
56312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23796 KB |
Output is correct |
3 |
Correct |
14 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23892 KB |
Output is correct |
5 |
Correct |
12 ms |
23812 KB |
Output is correct |
6 |
Correct |
12 ms |
23812 KB |
Output is correct |
7 |
Correct |
12 ms |
23748 KB |
Output is correct |
8 |
Correct |
13 ms |
23812 KB |
Output is correct |
9 |
Correct |
14 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23812 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23764 KB |
Output is correct |
13 |
Correct |
13 ms |
23944 KB |
Output is correct |
14 |
Correct |
13 ms |
24148 KB |
Output is correct |
15 |
Correct |
13 ms |
24020 KB |
Output is correct |
16 |
Correct |
13 ms |
23840 KB |
Output is correct |
17 |
Correct |
13 ms |
23824 KB |
Output is correct |
18 |
Correct |
15 ms |
24140 KB |
Output is correct |
19 |
Correct |
15 ms |
24148 KB |
Output is correct |
20 |
Correct |
15 ms |
24192 KB |
Output is correct |
21 |
Correct |
15 ms |
24148 KB |
Output is correct |
22 |
Correct |
14 ms |
23924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23796 KB |
Output is correct |
3 |
Correct |
14 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23892 KB |
Output is correct |
5 |
Correct |
12 ms |
23812 KB |
Output is correct |
6 |
Correct |
12 ms |
23812 KB |
Output is correct |
7 |
Correct |
12 ms |
23748 KB |
Output is correct |
8 |
Correct |
13 ms |
23812 KB |
Output is correct |
9 |
Correct |
14 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23812 KB |
Output is correct |
11 |
Correct |
331 ms |
55084 KB |
Output is correct |
12 |
Correct |
303 ms |
51924 KB |
Output is correct |
13 |
Correct |
751 ms |
56132 KB |
Output is correct |
14 |
Correct |
347 ms |
39996 KB |
Output is correct |
15 |
Correct |
764 ms |
56200 KB |
Output is correct |
16 |
Correct |
781 ms |
56312 KB |
Output is correct |
17 |
Correct |
13 ms |
23764 KB |
Output is correct |
18 |
Correct |
14 ms |
23764 KB |
Output is correct |
19 |
Correct |
13 ms |
23944 KB |
Output is correct |
20 |
Correct |
13 ms |
24148 KB |
Output is correct |
21 |
Correct |
13 ms |
24020 KB |
Output is correct |
22 |
Correct |
13 ms |
23840 KB |
Output is correct |
23 |
Correct |
13 ms |
23824 KB |
Output is correct |
24 |
Correct |
15 ms |
24140 KB |
Output is correct |
25 |
Correct |
15 ms |
24148 KB |
Output is correct |
26 |
Correct |
15 ms |
24192 KB |
Output is correct |
27 |
Correct |
15 ms |
24148 KB |
Output is correct |
28 |
Correct |
14 ms |
23924 KB |
Output is correct |
29 |
Correct |
12 ms |
23792 KB |
Output is correct |
30 |
Correct |
341 ms |
68576 KB |
Output is correct |
31 |
Correct |
347 ms |
68428 KB |
Output is correct |
32 |
Correct |
874 ms |
331824 KB |
Output is correct |
33 |
Correct |
394 ms |
209480 KB |
Output is correct |
34 |
Correct |
793 ms |
69576 KB |
Output is correct |
35 |
Correct |
797 ms |
69508 KB |
Output is correct |
36 |
Execution timed out |
5076 ms |
289092 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |