#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 <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(b);
adj[b].push_back(a);
}
}
int cnt;
int dfs(int ver, int par, int x)
{
int cover = 0, need_to_cover = 0;
for (auto i: adj[ver])
if (i != par)
{
int curr = dfs(i, ver, x);
if (curr > 0)
cover = max(cover, curr);
else
if (curr < 0)
need_to_cover = max(need_to_cover, -curr);
}
if (need_to_cover >= x)
{
cnt++;
return x;
}
need_to_cover = max(need_to_cover, (int)is[ver]);
if (need_to_cover == 0)
return max(0, cover - 1);
else
if (cover >= need_to_cover)
return max(0, cover - 1);
else
return -(need_to_cover + 1);
}
bool can(int x)
{
cnt = 0;
if (dfs(1, -1, x) < 0)
cnt++;
return cnt <= m;
}
void solve()
{
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 |
Correct |
5 ms |
7404 KB |
Output is correct |
4 |
Incorrect |
6 ms |
7404 KB |
Output isn't correct |
# |
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 |
7532 KB |
Output isn't correct |
# |
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 |
7 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
7916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
49 ms |
9708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
226 ms |
11292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
762 ms |
14660 KB |
Output is correct |
2 |
Incorrect |
868 ms |
18964 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1478 ms |
20196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1353 ms |
26468 KB |
Output is correct |
2 |
Incorrect |
1211 ms |
22588 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |