This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |