#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lsb(x) (x & (-x))
using namespace std;
const int INF = 1e9;
const int MAXN = (int) 3e5;
vector <int> g[MAXN + 1];
int dyn[MAXN + 1];
int dp1[MAXN + 1]; // cel mai departat TNT neexplodat
int dp2[MAXN + 1]; // cel mai apropiat nod selectat
int cnt;
void dfs(int nod, int par, int len) {
dp1[nod] = (dyn[nod] ? 0 : -INF);
dp2[nod] = -INF;
for(auto it : g[nod]) {
if(it != par) {
dfs(it, nod, len);
dp1[nod] = max(dp1[nod], dp1[it] + 1);
dp2[nod] = max(dp2[nod], dp2[it] - 1);
}
}
if(dp1[nod] <= dp2[nod]) dp1[nod] = -INF;
if(dp1[nod] >= len) {
dp1[nod] = -INF;
cnt++;
dp2[nod] = len;
}
}
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i, n, m;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for(i = 1; i <= n; i++) {
cin >> dyn[i];
}
for(i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
int res = -1;
for(int step = 1 << 18; step; step >>= 1) {
cnt = 0;
dfs(1, 0, res + step);
cnt += (dp1[1] >= 0);
if(cnt > m) {
res += step;
}
}
cout << res + 1;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7424 KB |
Output is correct |
3 |
Correct |
8 ms |
7424 KB |
Output is correct |
4 |
Correct |
7 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7344 KB |
Output is correct |
2 |
Correct |
8 ms |
7424 KB |
Output is correct |
3 |
Correct |
8 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
8064 KB |
Output is correct |
2 |
Correct |
43 ms |
8696 KB |
Output is correct |
3 |
Correct |
45 ms |
9088 KB |
Output is correct |
4 |
Correct |
61 ms |
10800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
10492 KB |
Output is correct |
2 |
Correct |
95 ms |
11648 KB |
Output is correct |
3 |
Correct |
376 ms |
11924 KB |
Output is correct |
4 |
Correct |
277 ms |
14840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
280 ms |
13304 KB |
Output is correct |
2 |
Correct |
258 ms |
13432 KB |
Output is correct |
3 |
Correct |
522 ms |
13048 KB |
Output is correct |
4 |
Correct |
605 ms |
18376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
902 ms |
19276 KB |
Output is correct |
2 |
Correct |
1136 ms |
21520 KB |
Output is correct |
3 |
Correct |
1667 ms |
29360 KB |
Output is correct |
4 |
Correct |
1585 ms |
29360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1953 ms |
27468 KB |
Output is correct |
2 |
Correct |
1454 ms |
25764 KB |
Output is correct |
3 |
Correct |
2005 ms |
30328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1759 ms |
33756 KB |
Output is correct |
2 |
Correct |
1345 ms |
25744 KB |
Output is correct |
3 |
Correct |
2009 ms |
35832 KB |
Output is correct |
4 |
Correct |
620 ms |
26056 KB |
Output is correct |