Submission #322561

# Submission time Handle Problem Language Result Execution time Memory
322561 2020-11-14T21:49:06 Z thecodingwizard Dynamite (POI11_dyn) C++11
100 / 100
1860 ms 36204 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define ii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define F0R(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define inf 1000000010

#define maxn 300000

int n, m; 
vector<int> adj[maxn];
bool lit[maxn];

int need[maxn]; // # of dynamites needed
int farthestDyn[maxn]; // farthest dynamite in subtree of i
int extraExplosion[maxn]; // extra explosion coming out of subtree of i

void dfs(int u, int p, int t) {
    for (int v : adj[u]) {
        if (v != p) {
            dfs(v, u, t);
        }
    }
    int extra = 0;
    for (int v : adj[u]) {
        if (v == p) continue;
        extra = max(extra, extraExplosion[v]);
    }
    --extra;
    int farDyn = lit[u] ? 0 : -inf;
    for (int v : adj[u]) {
        if (v == p) continue;
        farDyn = max(farDyn, farthestDyn[v] + 1);
    }
    if (farDyn <= extra) {
        farDyn = -inf;
    }
    assert(farDyn <= t);
    int totNeed = 0;
    for (int v : adj[u]) {
        if (v == p) continue;
        totNeed += need[v];
    }
    if (farDyn == t) {
        farDyn = -inf;
        extra = t;
        totNeed++;
    }

    need[u] = totNeed;
    farthestDyn[u] = farDyn;
    extraExplosion[u] = extra;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    cin >> n >> m;
    F0R(i, n) cin >> lit[i];
    F0R(i, n-1) {
        int a, b; cin >> a >> b; --a; --b;
        adj[a].pb(b);
        adj[b].pb(a);
    }

    int lo = 0, hi = n, mid, ans = -1;

    while (lo < hi) {
        mid = (lo+hi)/2;
        dfs(0, 0, mid);
        if (farthestDyn[0] >= 0) need[0]++;
        if (need[0] <= m) {
            hi = mid;
            ans = mid;
        } else {
            lo = mid+1;
        }
    }

    cout << ans << endl;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 4 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 6 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8044 KB Output is correct
2 Correct 33 ms 8812 KB Output is correct
3 Correct 38 ms 9088 KB Output is correct
4 Correct 49 ms 10860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 10604 KB Output is correct
2 Correct 142 ms 11756 KB Output is correct
3 Correct 156 ms 12140 KB Output is correct
4 Correct 218 ms 14956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 253 ms 13408 KB Output is correct
2 Correct 271 ms 13548 KB Output is correct
3 Correct 326 ms 13292 KB Output is correct
4 Correct 427 ms 18548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 857 ms 19564 KB Output is correct
2 Correct 992 ms 21752 KB Output is correct
3 Correct 1563 ms 29548 KB Output is correct
4 Correct 1540 ms 29692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1729 ms 27800 KB Output is correct
2 Correct 1412 ms 26092 KB Output is correct
3 Correct 1842 ms 30700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1777 ms 34104 KB Output is correct
2 Correct 1383 ms 26092 KB Output is correct
3 Correct 1860 ms 36204 KB Output is correct
4 Correct 599 ms 26460 KB Output is correct