Submission #897828

# Submission time Handle Problem Language Result Execution time Memory
897828 2024-01-03T18:19:16 Z box Amusement Park (JOI17_amusement_park) C++17
0 / 100
19 ms 3324 KB
#include "Joi.h"
#include <bits/stdc++.h>

using std::cerr, std::endl;

#ifdef LOCAL
template <class T, class... U> void bug_h(const T& t, const U&... u) { cerr << t; ((cerr << " | " << u), ...); cerr << endl; }
#define bug(...) cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: ", bug_h(__VA_ARGS__)
#else
#define cerr if (0) cerr
#define bug(...)
#endif

#define ar array
#define all(v) std::begin(v), std::end(v)
#define sz(v) int(std::size(v))
typedef long long i64;
typedef std::vector<int> vi;

namespace {

vi uf;

int find(int i) {
    return uf[i] == i ? i : uf[i] = find(uf[i]);
}
bool join(int i, int j) {
    i = find(i), j = find(j);
    if (i == j) return 0;
    uf[i] = uf[j] = i;
    return 1;
}

}

void Joi(int N, int M, int A[], int B[], long long X, int T) {
    uf.resize(N);
    std::iota(all(uf), 0);
    std::vector<vi> adj(N);
    for (int i = 0; i < M; i++) if (join(A[i], B[i])) {
        adj[A[i]].push_back(B[i]);
        adj[B[i]].push_back(A[i]);
    }
    vi dist(N, -1);
    vi parent(N, -1);
    auto bfs = [&](int i) {
        std::fill(all(dist), -1);
        std::queue<int> qu;
        dist[i] = 0; qu.push(i);
        while (sz(qu)) {
            int i = qu.front(); qu.pop();
            for (int j : adj[i]) if (dist[j] == -1) {
                dist[j] = dist[i] + 1;
                parent[j] = i;
                qu.push(j);
            }
        }
    };
    bfs(0);
    int u = std::max_element(all(dist)) - std::begin(dist);
    bfs(u);
    int v = std::max_element(all(dist)) - std::begin(dist);
    if (dist[v] >= 59) {
        for (int i = 0; i < N; i++)
            MessageBoard(i, X >> (dist[i] % 60) & 1);
    } else {
        vi par = parent;
        vi diameter = {v};
        while (diameter.back() != u) diameter.push_back(par[diameter.back()]);
        std::fill(all(dist), -1);
        vi qu = diameter;
        for (int x : diameter) dist[x] = 0;
        for (int i = 0; i < sz(qu); i++) {
            int u = qu[i];
            for (int v : adj[u]) if (dist[v] == -1) {
                dist[v] = dist[u] + 1;
                qu.push_back(v);
            }
        }
        assert(sz(qu) >= 60);
        qu.resize(60);
        vi color(N);
        for (int i = 0; i < 60; i++) {
            color[qu[i]] = X >> i & 1;
            bug(qu[i], i, X >> i & 1);
        }
        for (int i = 0; i < N; i++) MessageBoard(i, color[i]);
    }
}
#include "Ioi.h"
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
template <class T, class... U> void bug_h(const T& t, const U&... u) { cerr << t; ((cerr << " | " << u), ...); cerr << endl; }
#define bug(...) cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: ", bug_h(__VA_ARGS__)
#else
#define cerr if (0) cerr
#define bug(...)
#endif

#define ar array
#define all(v) std::begin(v), std::end(v)
#define sz(v) int(std::size(v))
typedef long long i64;
typedef vector<int> vi;
typedef pair<int, int> pi;

namespace {

vi uf;

int find(int i) {
    return uf[i] == i ? i : uf[i] = find(uf[i]);
}
bool join(int i, int j) {
    i = find(i), j = find(j);
    if (i == j) return 0;
    uf[i] = uf[j] = i;
    return 1;
}

}

long long Ioi(int N, int M, int A[], int B[], int P, int V, int T) {
    uf.resize(N);
    iota(all(uf), 0);
    vector<vi> adj(N);
    for (int i = 0; i < M; i++) if (join(A[i], B[i])) {
        adj[A[i]].push_back(B[i]);
        adj[B[i]].push_back(A[i]);
    }
    vi dist(N, -1);
    vi parent(N, -1);
    auto bfs = [&](int i) {
        fill(all(dist), -1);
        queue<int> qu;
        dist[i] = 0; qu.push(i);
        while (sz(qu)) {
            int i = qu.front(); qu.pop();
            for (int j : adj[i]) if (dist[j] == -1) {
                dist[j] = dist[i] + 1;
                parent[j] = i;
                qu.push(j);
            }
        }
    };
    bfs(0);
    int u = max_element(all(dist)) - begin(dist);
    bfs(u);
    int v = max_element(all(dist)) - begin(dist);
    auto save = dist;
    vi diameter = {v};
    vector<pi> edges;
    vi par = parent;
    while (diameter.back() != u) {
        int v = diameter.back();
        edges.push_back({v, par[v]});
        diameter.push_back(par[v]);
    }
    if (dist[v] >= 59) {
        i64 X = i64(V) << (dist[P] % 60);
        if (dist[P] >= 59) {
            for (int it = 0; it < 59; it++) {
                P = parent[P];
                X |= i64(Move(P)) << (dist[P] % 60);
            }
        } else {
            while (P != u) {
                P = parent[P];
                X |= i64(Move(P)) << (dist[P] % 60);
            }
            for (int i = 1; i < 60; i++) {
                P = diameter[sz(diameter) - i];
                X |= i64(Move(P)) << (dist[P] % 60);
            }
        }
        return X;
    } else {
        vi diameter = {v};
        vector<pi> edges;
        vi par = parent;
        while (diameter.back() != u) {
            int v = diameter.back();
            edges.push_back({v, par[v]});
            diameter.push_back(par[v]);
        }
        fill(all(par), -1);
        fill(all(dist), -1);
        vi qu = diameter;
        for (int x : diameter) dist[x] = 0;
        for (int i = 0; i < sz(qu); i++) {
            int u = qu[i];
            for (int v : adj[u]) if (dist[v] == -1) {
                dist[v] = dist[u] + 1;
                par[v] = u;
                if (sz(qu) < 60)
                    edges.push_back({u, v});
                qu.push_back(v);
            }
        }
        assert(sz(qu) >= 60);
        vi index(N, -1);
        for (int i = 0; i < 60; i++) {
            index[qu[i]] = i;
            // bug(qu[i], i);
        }
        i64 X = 0;
        if (dist[P] == 0) {
            X |= i64(V) << index[P];
        }
        while (dist[P] != 0) {
            P = par[P];
            assert(~P && P < N);
            int x = Move(P);
            if (dist[P] == 0) {
                bug(x, index[P]);
                X |= i64(x) << index[P];
            }
            bug(P, dist[P]);
        }
        assert(sz(edges) == 59);
        vector<vi> adj(N);
        for (auto [i, j] : edges) {
            if (i == 5 || j == 5) bug(i, j);
            adj[i].push_back(j), adj[j].push_back(i);
        }
        int last = save[P] * 2 > save[v] ? u : v;
        vector<bool> has(N);
        auto dfs = [&](auto dfs, int i) -> bool {
            has[i] = i == last; int who = -1;
            for (int j : adj[i]) {
                adj[j].erase(find(all(adj[j]), i));
                if (dfs(dfs, j)) {
                    has[i] = 1;
                    who = j;
                }
            }
            if (has[i] && sz(adj[i])) {
                assert(~who);
                int pos = find(all(adj[i]), who) - begin(adj[i]);
                swap(adj[i][pos], adj[i].back());
            }
            return has[i];
        };
        dfs(dfs, P);
        int cnt = 0;
        i64 Y = 0;
        bug(P);
        auto make = [&](auto make, int i) -> void {
            ++cnt;
            for (int j : adj[i]) {
                assert(~i && i < N);
                assert(~j && j < N);
                bug(j, index[j]);
                Y |= i64(1) << index[j];
                int v = Move(j);
                X |= i64(v) << index[j];
                // if (v) bug(j, index[j]);
                make(make, j);
                if (!has[j]) Move(i);
            }
        };
        make(make, P);
        bug(cnt);
        bug(Y);
        return X;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 792 KB Output is correct
2 Correct 1 ms 784 KB Output is correct
3 Correct 1 ms 800 KB Output is correct
4 Incorrect 0 ms 784 KB Wrong Answer [7]
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 3200 KB Output is correct
2 Incorrect 16 ms 3196 KB Wrong Answer [7]
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 780 KB Output is correct
2 Incorrect 0 ms 792 KB Wrong Answer [7]
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 3256 KB Output is correct
2 Correct 17 ms 3208 KB Output is correct
3 Correct 17 ms 3200 KB Output is correct
4 Correct 11 ms 3316 KB Output is correct
5 Incorrect 10 ms 3068 KB Wrong Answer [7]
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 3192 KB Output is correct
2 Correct 18 ms 3200 KB Output is correct
3 Correct 17 ms 3216 KB Output is correct
4 Correct 11 ms 3300 KB Output is correct
5 Correct 11 ms 3064 KB Output is correct
6 Correct 11 ms 2808 KB Output is correct
7 Correct 10 ms 2808 KB Output is correct
8 Correct 11 ms 2820 KB Output is correct
9 Correct 12 ms 2820 KB Output is correct
10 Correct 9 ms 3308 KB Output is correct
11 Correct 9 ms 3320 KB Output is correct
12 Correct 9 ms 3296 KB Output is correct
13 Correct 10 ms 3280 KB Output is correct
14 Correct 10 ms 3300 KB Output is correct
15 Correct 11 ms 3304 KB Output is correct
16 Correct 11 ms 3316 KB Output is correct
17 Correct 13 ms 3320 KB Output is correct
18 Correct 12 ms 3324 KB Output is correct
19 Correct 11 ms 3320 KB Output is correct
20 Correct 8 ms 2828 KB Output is correct
21 Correct 8 ms 2808 KB Output is correct
22 Correct 13 ms 2804 KB Output is correct
23 Correct 11 ms 2816 KB Output is correct
24 Correct 11 ms 2812 KB Output is correct
25 Correct 11 ms 2820 KB Output is correct
26 Correct 10 ms 2812 KB Output is correct
27 Correct 11 ms 2816 KB Output is correct
28 Correct 10 ms 2804 KB Output is correct
29 Correct 12 ms 2820 KB Output is correct
30 Correct 10 ms 2820 KB Output is correct
31 Correct 1 ms 796 KB Output is correct
32 Correct 0 ms 788 KB Output is correct
33 Correct 0 ms 800 KB Output is correct
34 Correct 0 ms 796 KB Output is correct
35 Incorrect 0 ms 800 KB Wrong Answer [7]
36 Halted 0 ms 0 KB -