#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'
using namespace std;
const int maxn = 3e5 + 3;
int n, m;
bool is[maxn];
vector <pair <int, int> > adj[maxn];
void read()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> is[i];
for (int i = 1; i <= n-1; i++)
{
int a, b;
cin >> a >> b;
adj[a].push_back({0, b});
adj[b].push_back({0, a});
}
}
int depth[maxn], max_depth[maxn];
void pre_dfs(int ver, int par)
{
max_depth[ver] = -1;
if (is[ver])
max_depth[ver] = depth[ver];
for (auto i: adj[ver])
if (i.second != par)
{
depth[i.second] = depth[ver] + 1;
pre_dfs(i.second, ver);
max_depth[ver] = max(max_depth[ver], max_depth[i.second]);
}
}
int cnt;
int dfs(int ver, int par, int cover, int x)
{
if (max_depth[ver] - depth[ver] <= cover)
return 0;
if (max_depth[ver] - depth[ver] <= x)
{
cnt++;
return x;
}
int curr_cover = cover;
for (auto i: adj[ver])
if (i.second != par)
curr_cover = max(curr_cover, dfs(i.second, ver, max(0, curr_cover - 1), x));
if (curr_cover == 0 && is[ver])
{
cnt++;
return x;
}
return curr_cover - 1;
}
bool can(int x)
{
cnt = 0;
dfs(1, -1, 0, x);
return cnt <= m;
}
void solve()
{
depth[1] = 1;
pre_dfs(1, -1);
for (int i = 1; i <= n; i++)
{
for (auto &j: adj[i])
j.first = max_depth[j.second];
sort (adj[i].begin(), adj[i].end());
reverse(adj[i].begin(), adj[i].end());
}
int low = 0, high = n;
while (low < high)
{
int mid = (low + high) / 2;
if (can(mid))
high = mid;
else
low = mid+1;
}
cout << low << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7404 KB |
Output is correct |
3 |
Incorrect |
5 ms |
7404 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Incorrect |
5 ms |
7404 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
7404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
7404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
7948 KB |
Output is correct |
2 |
Correct |
18 ms |
8680 KB |
Output is correct |
3 |
Correct |
23 ms |
9452 KB |
Output is correct |
4 |
Incorrect |
41 ms |
10732 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
10732 KB |
Output is correct |
2 |
Correct |
54 ms |
11564 KB |
Output is correct |
3 |
Correct |
69 ms |
11876 KB |
Output is correct |
4 |
Incorrect |
167 ms |
15008 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
13560 KB |
Output is correct |
2 |
Correct |
79 ms |
13284 KB |
Output is correct |
3 |
Correct |
88 ms |
12900 KB |
Output is correct |
4 |
Incorrect |
296 ms |
18660 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
212 ms |
19680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
513 ms |
27236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
510 ms |
36068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |