#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair
const int MX = 300005;
const int MOD = (int) (1e9 + 7);
const ll INF = (ll) 1e18;
namespace output {
void pr(int x) {
cout << x;
}
void pr(long x) {
cout << x;
}
void pr(ll x) {
cout << x;
}
void pr(unsigned x) {
cout << x;
}
void pr(unsigned long x) {
cout << x;
}
void pr(unsigned long long x) {
cout << x;
}
void pr(float x) {
cout << x;
}
void pr(double x) {
cout << x;
}
void pr(long double x) {
cout << x;
}
void pr(char x) {
cout << x;
}
void pr(const char * x) {
cout << x;
}
void pr(const string & x) {
cout << x;
}
void pr(bool x) {
pr(x ? "true" : "false");
}
template < class T1, class T2 > void pr(const pair < T1, T2 > & x);
template < class T > void pr(const T & x);
template < class T, class...Ts > void pr(const T & t,
const Ts & ...ts) {
pr(t);
pr(ts...);
}
template < class T1, class T2 > void pr(const pair < T1, T2 > & x) {
pr("{", x.f, ", ", x.s, "}");
}
template < class T > void pr(const T & x) {
pr("{"); // const iterator needed for vector<bool>
bool fst = 1;
for (const auto & a: x) pr(!fst ? ", " : "", a), fst = 0;
pr("}");
}
void ps() {
pr("\n");
} // print w/ spaces
template < class T, class...Ts > void ps(const T & t,
const Ts & ...ts) {
pr(t);
if (sizeof...(ts)) pr(" ");
ps(ts...);
}
void pc() {
cout << "]" << endl;
} // debug w/ commas
template < class T, class...Ts > void pc(const T & t,
const Ts & ...ts) {
pr(t);
if (sizeof...(ts)) pr(", ");
pc(ts...);
}
#define dbg(x...) pr("[", #x, "] = ["), pc(x);
}
#ifdef LOCAL
using namespace output;
#endif
int n, m;
int d[MX];
vector<int> adj[MX];
int dp1[MX], dp2[MX];
int need = 0;
void dfs(int v, int p, int mid) {
dp1[v] = -1;
dp2[v] = 987654321;
for (int to : adj[v]) {
if (to != p) {
dfs(to, v, mid);
dp2[v] = min(dp2[v], dp2[to] + 1);
}
}
for (int to : adj[v]) {
if (to != p) {
// dbg(v + 1, to + 1);
if (dp1[to] + 1 + dp2[v] <= mid) {
continue;
}
if (dp1[to] != -1) {
dp1[v] = max(dp1[v], dp1[to] + 1);
}
}
}
if (dp1[v] == -1 && d[v] && dp2[v] > mid) {
dp1[v] = 0;
}
if (dp1[v] == mid || (v == 0 && dp1[v] != -1)) {
dp2[v] = 0;
dp1[v] = -1;
++need;
}
// dbg(v + 1, dp1[v], dp2[v]);
return;
}
bool solve(int mid) {
dfs(0, -1, mid);
// dbg(mid, need);
if (need > m) {
return false;
}
return true;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
int cnt = 0;
for (int i = 0; i < n; i++) {
cin >> d[i];
cnt += d[i];
}
for (int i = 0; i < n - 1; i++) {
int u, v; cin >> u >> v;
u--, v--;
adj[u].pb(v), adj[v].pb(u);
}
int lo = 1, hi = n;
int ans = 0;
while (lo <= hi) {
int mid = (lo + hi) / 2;
need = 0;
if (solve(mid)) {
hi = mid - 1;
ans = mid;
}
else {
lo = mid + 1;
}
}
if (m >= cnt) {
cout << 0 << '\n';
}
else {
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
7424 KB |
Output is correct |
2 |
Correct |
6 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7424 KB |
Output is correct |
2 |
Correct |
6 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
7808 KB |
Output is correct |
2 |
Correct |
34 ms |
8448 KB |
Output is correct |
3 |
Correct |
40 ms |
9208 KB |
Output is correct |
4 |
Correct |
47 ms |
11136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
9852 KB |
Output is correct |
2 |
Correct |
136 ms |
10616 KB |
Output is correct |
3 |
Correct |
196 ms |
11000 KB |
Output is correct |
4 |
Correct |
231 ms |
14328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
262 ms |
11896 KB |
Output is correct |
2 |
Correct |
270 ms |
12156 KB |
Output is correct |
3 |
Correct |
338 ms |
11772 KB |
Output is correct |
4 |
Correct |
432 ms |
17912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
821 ms |
16468 KB |
Output is correct |
2 |
Correct |
942 ms |
21496 KB |
Output is correct |
3 |
Correct |
1454 ms |
30840 KB |
Output is correct |
4 |
Correct |
1451 ms |
30960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1741 ms |
23508 KB |
Output is correct |
2 |
Correct |
1491 ms |
25848 KB |
Output is correct |
3 |
Correct |
1688 ms |
31352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1730 ms |
31224 KB |
Output is correct |
2 |
Correct |
1474 ms |
25996 KB |
Output is correct |
3 |
Correct |
1879 ms |
38008 KB |
Output is correct |
4 |
Correct |
557 ms |
26212 KB |
Output is correct |