#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 cnt[maxn];
void remove_vertex(int ver, int x)
{
for (int i = 1; i <= n; i++)
if (dist[ver][i] <= x)
cnt[i]--;
}
bool used[maxn], is_covered[maxn];
bool can(int x)
{
priority_queue <pair <int, int> > q;
for (int i = 1; i <= n; i++)
{
cnt[i] = 0;
for (auto j: v)
if (dist[j][i] <= x)
cnt[i]++;
}
for (int i = 1; i <= n; i++)
used[i] = false;
for (auto i: v)
is_covered[i] = false;
for (int i = 1; i <= m; i++)
{
int maxs = -1, idx = -1;
for (int j = 1; j <= n; j++)
if (!used[j])
if (cnt[j] > maxs)
maxs = cnt[j], idx = j;
used[idx] = true;
for (auto i: v)
if (!is_covered[i] && dist[idx][i] <= x)
{
is_covered[i] = true;
remove_vertex(i, x);
}
}
for (auto i: v)
if (!is_covered[i])
return false;
return true;
}
void solve()
{
for (int i = 1; i <= n; i++)
{
dist[i][i] = 0;
dfs(i, i, -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 |
364 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 |
676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
2412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
3564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
748 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 |
6 ms |
876 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 |
1388 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 |
2920 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 |
27 ms |
2284 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 |
28 ms |
3932 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |