# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
313819 | BeanZ | Untitled (POI11_dyn) | C++14 | 1997 ms | 36984 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// I_Love_LPL
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define endl '\n'
const int N = 3e5 + 5;
ll cnt = 0, mid;
ll dp[N], hp[N], a[N], farcry[N];
vector<ll> node[N];
void dfs(ll u, ll p){
hp[u] = 0;
for (auto j : node[u]){
if (j == p) continue;
dfs(j, u);
farcry[u] = min(farcry[u], farcry[j] + 1);
}
if (a[u] == 1 && farcry[u] > mid) hp[u] = 1, dp[u] = 0;
for (auto j : node[u]){
if (j == p) continue;
if (hp[j]){
if ((farcry[u] + dp[j] + 1) <= mid) continue;
else dp[u] = max(dp[u], dp[j] + 1), hp[u] = 1;
}
}
if (dp[u] == mid){
cnt++;
dp[u] = -1e9;
farcry[u] = 0;
hp[u] = 0;
}
if (u == 1){
if (dp[u] == 0 && a[u] && farcry[u] > mid) cnt++;
else if (dp[u] >= 1) cnt++;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("A.inp", "r")){
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
ll n, m;
cin >> n >> m;
ll res = 0;
for (int i = 1; i <= n; i++){
cin >> a[i];
if (a[i] == 1) res++;
}
for (int i = 1; i < n; i++){
ll u, v;
cin >> u >> v;
node[u].push_back(v);
node[v].push_back(u);
}
if (res <= m) return cout << 0, 0;
ll l = 1, h = n;
while (l <= h){
mid = (l + h) >> 1;
cnt = 0;
memset(hp, 0, sizeof(hp));
for (int i = 1; i <= n; i++) farcry[i] = 1e9, dp[i] = -1e9;
dfs(1, 1);
if (cnt > m) l = mid + 1;
else h = mid - 1;
}
cout << l;
}
/*
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |