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 <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;
pair <int, int> dfs(int ver, int par, int cover, int x)
{
if (max_depth[ver] - depth[ver] <= cover)
return {0, 0};
if (max_depth[ver] - depth[ver] <= x)
{
cnt++;
return {x, 0};
}
int curr_cover = cover, curr_max_depth = 0;
for (auto i: adj[ver])
if (i.second != par)
{
auto curr = dfs(i.second, ver, max(0, curr_cover - 1), x);
curr_cover = max(curr_cover, curr.first);
curr_max_depth = max(curr_max_depth, curr.second);
}
if (is[ver])
curr_max_depth = max(curr_max_depth, depth[ver]);
if (curr_max_depth - depth[ver] < curr_cover)
curr_max_depth = 0;
else
if (curr_max_depth == 1)
{
cnt++;
return {0, 0};
}
else
if (curr_max_depth - depth[ver] == x)
{
cnt++;
return {x, 0};
}
else
return {curr_cover - 1, curr_max_depth};
}
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();
}
Compilation message (stderr)
dyn.cpp: In function 'std::pair<int, int> dfs(int, int, int, int)':
dyn.cpp:69:20: warning: control reaches end of non-void function [-Wreturn-type]
69 | curr_max_depth = 0;
| ~~~~~~~~~~~~~~~^~~
# | 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... |