#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] = d[v] ? 0 : -1;
dp2[v] = 987654321;
for (int to : adj[v]) {
if (to != p) {
dfs(to, v, mid);
if (dp1[to] != -1) {
dp1[v] = max(dp1[v], dp1[to] + 1);
}
dp2[v] = min(dp2[v], dp2[to] + 1);
}
}
// dbg(v, dp1[v], dp2[v]);
if (dp2[v] <= mid) {
dp1[v] = -1;
}
if (dp1[v] == mid || (v == 0 && dp1[v] != -1)) {
dp2[v] = 0;
dp1[v] = -1;
++need;
}
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 |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
6 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
7424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
7424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
7808 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
9856 KB |
Output is correct |
2 |
Correct |
165 ms |
10676 KB |
Output is correct |
3 |
Correct |
217 ms |
10872 KB |
Output is correct |
4 |
Incorrect |
187 ms |
13800 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
12024 KB |
Output is correct |
2 |
Correct |
255 ms |
12024 KB |
Output is correct |
3 |
Correct |
274 ms |
11784 KB |
Output is correct |
4 |
Incorrect |
355 ms |
17144 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
809 ms |
16376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1616 ms |
23256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1479 ms |
29560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |