Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int INF = 1e9 + 7;
- const int N = 105;
- int n, m;
- bool a[N];
- int graph[N][N];
- int d[N][N];
- void Floyd() {
- for (int i = 1; i <= n; ++i)
- for (int j = 1; j <= n; ++j)
- if (i != j)
- d[i][j] = graph[i][j];
- for (int k = 1; k <= n; ++k)
- for (int i = 1; i <= n; ++i)
- for (int j = 1; j <= n; ++j)
- d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
- }
- void solve() {
- cin >> n >> m;
- for (int i = 1; i <= n; ++i)
- cin >> a[i];
- for (int i = 1; i <= n; ++i)
- for (int j = 1; j <= n; ++j)
- graph[i][j] = INF;
- for (int i = 1; i < n; ++i) {
- int u, v; cin >> u >> v;
- graph[u][v] = graph[v][u] = 1;
- }
- Floyd();
- int ans = n;
- if (m == 2) {
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= n; ++j) {
- if (i != j && a[i] && a[j])
- ans = min(ans, d[i][j]);
- }
- }
- cout << ans << '\n';
- } else {
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= n; ++j) {
- if (!a[i] || !a[j])
- continue;
- int inc = 0, mx = d[i][j];
- for (int k = 1; k <= n; ++k) {
- if (!a[k]) continue;
- if (d[i][k] <= mx && d[k][j] <= mx) ++inc;
- }
- if (inc >= m)
- ans = min(ans, mx);
- }
- }
- cout << ans << '\n';
- }
- return;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- solve();
- return 0;
- }
Add Comment
Please, Sign In to add comment