답안 #856605

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
856605 2023-10-04T03:37:45 Z vjudge1 Dynamite (POI11_dyn) C++17
0 / 100
134 ms 43600 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(0);

    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);
      |         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 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 10840 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 9 ms 21596 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 9 ms 21596 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 9 ms 21852 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 13 ms 22532 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 24 ms 25684 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 39 ms 29268 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 70 ms 36444 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 134 ms 43600 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 115 ms 43348 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -