#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'
using namespace std;
const int maxn = 1003;
int n, m;
vector <int> v;
vector <int> adj[maxn];
void read()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
bool is;
cin >> is;
if (is)
v.push_back(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 dist[maxn][maxn];
void dfs(int start, int ver, int par)
{
for (auto i: adj[ver])
if (i != par)
{
dist[start][i] = dist[start][ver] + 1;
dfs(start, i, ver);
}
}
int depth[maxn], pr[maxn];
void dfs1(int ver, int par)
{
for (auto i: adj[ver])
if (i != par)
{
pr[i] = ver;
depth[i] = depth[ver] + 1;
dfs1(i, ver);
}
}
bool is_covered[maxn];
bool can(int x)
{
vector <pair <int, int> > v1;
for (auto i: v)
v1.push_back({depth[i], i});
sort(v1.begin(), v1.end());
reverse(v1.begin(), v1.end());
for (int i = 1; i <= n; i++)
is_covered[i] = false;
int cnt = 0;
for (auto i: v1)
{
if (is_covered[i.second])
continue;
cnt++;
int curr = i.second;
for (int i = 1; i <= x && curr != 1; i++)
curr = pr[curr];
for (auto j: v)
if (dist[j][curr] <= x)
is_covered[j] = true;
}
return cnt <= m;
}
void solve()
{
for (int i = 1; i <= n; i++)
{
dist[i][i] = 0;
dfs(i, i, -1);
}
depth[1] = 1;
pr[1] = -1;
dfs1(1, -1);
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 |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2432 KB |
Output is correct |
2 |
Correct |
7 ms |
2796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
3564 KB |
Output is correct |
2 |
Correct |
10 ms |
4332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
620 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
5 ms |
620 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
10 ms |
1004 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
19 ms |
2280 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
26 ms |
1584 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
33 ms |
3164 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |