Submission #856604

# Submission time Handle Problem Language Result Execution time Memory
856604 2023-10-04T03:37:25 Z vjudge1 Dynamite (POI11_dyn) C++17
Compilation error
0 ms 0 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;
}

#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 8 * 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;
}

#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 8 * 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:99:5: error: redefinition of 'int n'
   99 | int n, m, d[N], f[N], h[N];
      |     ^
dyn.cpp:16:5: note: 'int n' previously declared here
   16 | int n, m, d[N], f[N], h[N];
      |     ^
dyn.cpp:99:8: error: redefinition of 'int m'
   99 | int n, m, d[N], f[N], h[N];
      |        ^
dyn.cpp:16:8: note: 'int m' previously declared here
   16 | int n, m, d[N], f[N], h[N];
      |        ^
dyn.cpp:99:11: error: redefinition of 'int d [300050]'
   99 | int n, m, d[N], f[N], h[N];
      |           ^
dyn.cpp:16:11: note: 'int d [300050]' previously declared here
   16 | int n, m, d[N], f[N], h[N];
      |           ^
dyn.cpp:99:17: error: redefinition of 'int f [300050]'
   99 | int n, m, d[N], f[N], h[N];
      |                 ^
dyn.cpp:16:17: note: 'int f [300050]' previously declared here
   16 | int n, m, d[N], f[N], h[N];
      |                 ^
dyn.cpp:99:23: error: redefinition of 'int h [300050]'
   99 | int n, m, d[N], f[N], h[N];
      |                       ^
dyn.cpp:16:23: note: 'int h [300050]' previously declared here
   16 | int n, m, d[N], f[N], h[N];
      |                       ^
dyn.cpp:100:13: error: redefinition of 'std::vector<int> g [300050]'
  100 | vector<int> g[N], dfn;
      |             ^
dyn.cpp:17:13: note: 'std::vector<int> g [300050]' previously declared here
   17 | vector<int> g[N], dfn;
      |             ^
dyn.cpp:100:19: error: redefinition of 'std::vector<int> dfn'
  100 | vector<int> g[N], dfn;
      |                   ^~~
dyn.cpp:17:19: note: 'std::vector<int> dfn' previously declared here
   17 | vector<int> g[N], dfn;
      |                   ^~~
dyn.cpp:102:13: error: redefinition of 'std::vector<int> rev_dfn(int)'
  102 | vector<int> rev_dfn(int src)
      |             ^~~~~~~
dyn.cpp:19:13: note: 'std::vector<int> rev_dfn(int)' previously defined here
   19 | vector<int> rev_dfn(int src)
      |             ^~~~~~~
dyn.cpp:119:5: error: redefinition of 'int ok(int)'
  119 | int ok(int y)
      |     ^~
dyn.cpp:36:5: note: 'int ok(int)' previously defined here
   36 | int ok(int y)
      |     ^~
dyn.cpp:144:5: error: redefinition of 'int main()'
  144 | int main()
      |     ^~~~
dyn.cpp:61:5: note: 'int main()' previously defined here
   61 | int main()
      |     ^~~~
dyn.cpp:182:5: error: redefinition of 'int n'
  182 | int n, m, d[N], f[N], h[N];
      |     ^
dyn.cpp:16:5: note: 'int n' previously declared here
   16 | int n, m, d[N], f[N], h[N];
      |     ^
dyn.cpp:182:8: error: redefinition of 'int m'
  182 | int n, m, d[N], f[N], h[N];
      |        ^
dyn.cpp:16:8: note: 'int m' previously declared here
   16 | int n, m, d[N], f[N], h[N];
      |        ^
dyn.cpp:182:11: error: redefinition of 'int d [300050]'
  182 | int n, m, d[N], f[N], h[N];
      |           ^
dyn.cpp:16:11: note: 'int d [300050]' previously declared here
   16 | int n, m, d[N], f[N], h[N];
      |           ^
dyn.cpp:182:17: error: redefinition of 'int f [300050]'
  182 | int n, m, d[N], f[N], h[N];
      |                 ^
dyn.cpp:16:17: note: 'int f [300050]' previously declared here
   16 | int n, m, d[N], f[N], h[N];
      |                 ^
dyn.cpp:182:23: error: redefinition of 'int h [300050]'
  182 | int n, m, d[N], f[N], h[N];
      |                       ^
dyn.cpp:16:23: note: 'int h [300050]' previously declared here
   16 | int n, m, d[N], f[N], h[N];
      |                       ^
dyn.cpp:183:13: error: redefinition of 'std::vector<int> g [300050]'
  183 | vector<int> g[N], dfn;
      |             ^
dyn.cpp:17:13: note: 'std::vector<int> g [300050]' previously declared here
   17 | vector<int> g[N], dfn;
      |             ^
dyn.cpp:183:19: error: redefinition of 'std::vector<int> dfn'
  183 | vector<int> g[N], dfn;
      |                   ^~~
dyn.cpp:17:19: note: 'std::vector<int> dfn' previously declared here
   17 | vector<int> g[N], dfn;
      |                   ^~~
dyn.cpp:185:13: error: redefinition of 'std::vector<int> rev_dfn(int)'
  185 | vector<int> rev_dfn(int src)
      |             ^~~~~~~
dyn.cpp:19:13: note: 'std::vector<int> rev_dfn(int)' previously defined here
   19 | vector<int> rev_dfn(int src)
      |             ^~~~~~~
dyn.cpp:202:5: error: redefinition of 'int ok(int)'
  202 | int ok(int y)
      |     ^~
dyn.cpp:36:5: note: 'int ok(int)' previously defined here
   36 | int ok(int y)
      |     ^~
dyn.cpp:227:5: error: redefinition of 'int main()'
  227 | int main()
      |     ^~~~
dyn.cpp:61:5: note: 'int main()' previously defined here
   61 | int main()
      |     ^~~~
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);
      |         ~~~~~^~~~~~~~~~~~~~~~
dyn.cpp: In function 'int main()':
dyn.cpp:147:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  147 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
dyn.cpp:148:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  148 |     for (int i = 1; i <= n; ++i) scanf("%d", d+i);
      |                                  ~~~~~^~~~~~~~~~~
dyn.cpp:151:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  151 |         scanf("%d%d", &u, &v), g[u].push_back(v), g[v].push_back(u);
      |         ~~~~~^~~~~~~~~~~~~~~~
dyn.cpp: In function 'int main()':
dyn.cpp:230:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  230 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
dyn.cpp:231:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  231 |     for (int i = 1; i <= n; ++i) scanf("%d", d+i);
      |                                  ~~~~~^~~~~~~~~~~
dyn.cpp:234:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  234 |         scanf("%d%d", &u, &v), g[u].push_back(v), g[v].push_back(u);
      |         ~~~~~^~~~~~~~~~~~~~~~