#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] = INT_MAX;
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);
}
}
if (dp1[v] == -1 && d[v]) {
if (dp2[v] > mid) {
dp1[v] = 0;
}
}
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);
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 |
5 ms |
7424 KB |
Output is correct |
4 |
Incorrect |
5 ms |
7424 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
6 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
7424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
7808 KB |
Output is correct |
2 |
Incorrect |
35 ms |
8448 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
9976 KB |
Output is correct |
2 |
Correct |
115 ms |
10632 KB |
Output is correct |
3 |
Correct |
215 ms |
10872 KB |
Output is correct |
4 |
Incorrect |
200 ms |
13688 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
223 ms |
12024 KB |
Output is correct |
2 |
Correct |
240 ms |
12152 KB |
Output is correct |
3 |
Incorrect |
285 ms |
11768 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
760 ms |
16376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1644 ms |
26912 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1450 ms |
33272 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |