Submission #856606

# Submission time Handle Problem Language Result Execution time Memory
856606 2023-10-04T03:38:17 Z vjudge1 Dynamite (POI11_dyn) C++17
30 / 100
355 ms 23636 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <numeric>
#include <array>
#include <cstring>

using namespace std;
#define ALL(x) x.begin(), x.end()
using ll = long long;
#define N 300050

int n, m, d[N], f[N], h[N];
vector<int> g[N], dfn;

vector<int> rev_dfn(int src)
{
    vector<int> st = {src}, V(n);
    for (int i = 0; i < n; ++i)
    {
        auto u = st.back(); st.pop_back();
        V[i] = u;
        for (auto v : g[u])
        {
            g[v].erase(find(ALL(g[v]), u));
            st.push_back(v);
        }
    }
    reverse(ALL(V));
    return V;
}

int ok(int y)
{
    memset(f, -1, sizeof f);
    memset(h, 63, sizeof h);
    int k = m;
    for (auto u : dfn)
    {
        for (auto v : g[u])
        {
            if (f[v] != -1) f[u] = max(f[u], f[v] + 1);
            h[u] = min(h[u], h[v] + 1);
        }

        if (f[u] == -1 && d[u]) f[u] = 0;

        if (f[u] == y)
        {
            if (!k--) return 0;
            f[u] = -1; h[u] = 0;
        } else if (f[u] + h[u] <= y)
            f[u] = -1;
    }
    return k || f[0] == -1;
}

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i) scanf("%d", d+i);
    if (accumulate(d, d+n+1, 0) <= m) return 0 * puts("0");
    for (int u, v, i = 1; i < n; ++i)
        scanf("%d%d", &u, &v), g[u].push_back(v), g[v].push_back(u);
    dfn = rev_dfn(1);

    int l = 1, r = 2*n, z = -1;
    while (l <= r)
    {
        int y = (l+r)/2;
        ok(y) ? z = y, r = y - 1 : l = y + 1;
    }

    printf("%d", z);

    
    return 0;
}

Compilation message

dyn.cpp: In function 'int main()':
dyn.cpp:64:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
dyn.cpp:65:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |     for (int i = 1; i <= n; ++i) scanf("%d", d+i);
      |                                  ~~~~~^~~~~~~~~~~
dyn.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |         scanf("%d%d", &u, &v), g[u].push_back(v), g[v].push_back(u);
      |         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Incorrect 2 ms 10844 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 10844 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10844 KB Output is correct
2 Correct 3 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11096 KB Output is correct
2 Correct 14 ms 12120 KB Output is correct
3 Correct 15 ms 12124 KB Output is correct
4 Correct 16 ms 12176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 12892 KB Output is correct
2 Correct 37 ms 14416 KB Output is correct
3 Correct 39 ms 14684 KB Output is correct
4 Incorrect 60 ms 14672 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 60 ms 14420 KB Output is correct
2 Correct 55 ms 15952 KB Output is correct
3 Correct 49 ms 15700 KB Output is correct
4 Incorrect 63 ms 16468 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 218 ms 18056 KB Output is correct
2 Correct 165 ms 22956 KB Output is correct
3 Incorrect 282 ms 23636 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 355 ms 21860 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 291 ms 21844 KB Output isn't correct
2 Halted 0 ms 0 KB -