답안 #897829

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
897829 2024-01-03T18:20:06 Z box Amusement Park (JOI17_amusement_park) C++17
100 / 100
17 ms 3908 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 - 1];
                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;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 784 KB Output is correct
2 Correct 0 ms 792 KB Output is correct
3 Correct 1 ms 800 KB Output is correct
4 Correct 0 ms 796 KB Output is correct
5 Correct 0 ms 788 KB Output is correct
6 Correct 1 ms 788 KB Output is correct
7 Correct 1 ms 796 KB Output is correct
8 Correct 0 ms 804 KB Output is correct
9 Correct 1 ms 804 KB Output is correct
10 Correct 1 ms 788 KB Output is correct
11 Correct 3 ms 1128 KB Output is correct
12 Correct 1 ms 1056 KB Output is correct
13 Correct 1 ms 800 KB Output is correct
14 Correct 1 ms 796 KB Output is correct
15 Correct 0 ms 800 KB Output is correct
16 Correct 1 ms 804 KB Output is correct
17 Correct 1 ms 804 KB Output is correct
18 Correct 1 ms 796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 3196 KB Output is correct
2 Correct 16 ms 3184 KB Output is correct
3 Correct 17 ms 3652 KB Output is correct
4 Correct 12 ms 3424 KB Output is correct
5 Correct 14 ms 3384 KB Output is correct
6 Correct 13 ms 3144 KB Output is correct
7 Correct 11 ms 3144 KB Output is correct
8 Correct 11 ms 3384 KB Output is correct
9 Correct 11 ms 3148 KB Output is correct
10 Correct 10 ms 3784 KB Output is correct
11 Correct 9 ms 3660 KB Output is correct
12 Correct 9 ms 3404 KB Output is correct
13 Correct 10 ms 3408 KB Output is correct
14 Correct 10 ms 3416 KB Output is correct
15 Correct 12 ms 3400 KB Output is correct
16 Correct 11 ms 3404 KB Output is correct
17 Correct 11 ms 3404 KB Output is correct
18 Correct 11 ms 3540 KB Output is correct
19 Correct 11 ms 3420 KB Output is correct
20 Correct 8 ms 3140 KB Output is correct
21 Correct 9 ms 3148 KB Output is correct
22 Correct 12 ms 3148 KB Output is correct
23 Correct 10 ms 3140 KB Output is correct
24 Correct 10 ms 3148 KB Output is correct
25 Correct 10 ms 3144 KB Output is correct
26 Correct 12 ms 3176 KB Output is correct
27 Correct 10 ms 3168 KB Output is correct
28 Correct 11 ms 3136 KB Output is correct
29 Correct 9 ms 2896 KB Output is correct
30 Correct 10 ms 3136 KB Output is correct
31 Correct 0 ms 784 KB Output is correct
32 Correct 1 ms 784 KB Output is correct
33 Correct 1 ms 804 KB Output is correct
34 Correct 1 ms 796 KB Output is correct
35 Correct 0 ms 788 KB Output is correct
36 Correct 0 ms 788 KB Output is correct
37 Correct 1 ms 784 KB Output is correct
38 Correct 0 ms 788 KB Output is correct
39 Correct 1 ms 800 KB Output is correct
40 Correct 1 ms 784 KB Output is correct
41 Correct 0 ms 784 KB Output is correct
42 Correct 0 ms 800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 784 KB Output is correct
2 Correct 0 ms 796 KB Output is correct
3 Correct 1 ms 792 KB Output is correct
4 Correct 2 ms 1336 KB Output is correct
5 Correct 2 ms 1336 KB Output is correct
6 Correct 2 ms 1336 KB Output is correct
7 Correct 2 ms 1328 KB Output is correct
8 Correct 2 ms 1336 KB Output is correct
9 Correct 8 ms 3012 KB Output is correct
10 Correct 8 ms 3068 KB Output is correct
11 Correct 8 ms 3320 KB Output is correct
12 Correct 0 ms 1292 KB Output is correct
13 Correct 0 ms 796 KB Output is correct
14 Correct 0 ms 796 KB Output is correct
15 Correct 3 ms 792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 3300 KB Output is correct
2 Correct 16 ms 3196 KB Output is correct
3 Correct 16 ms 3344 KB Output is correct
4 Correct 14 ms 3280 KB Output is correct
5 Correct 11 ms 3064 KB Output is correct
6 Correct 10 ms 3140 KB Output is correct
7 Correct 11 ms 3148 KB Output is correct
8 Correct 11 ms 3140 KB Output is correct
9 Correct 10 ms 3136 KB Output is correct
10 Correct 10 ms 3664 KB Output is correct
11 Correct 12 ms 3908 KB Output is correct
12 Correct 11 ms 3408 KB Output is correct
13 Correct 10 ms 3660 KB Output is correct
14 Correct 10 ms 3432 KB Output is correct
15 Correct 11 ms 3400 KB Output is correct
16 Correct 11 ms 3404 KB Output is correct
17 Correct 11 ms 3404 KB Output is correct
18 Correct 12 ms 3400 KB Output is correct
19 Correct 11 ms 3420 KB Output is correct
20 Correct 9 ms 3144 KB Output is correct
21 Correct 9 ms 3148 KB Output is correct
22 Correct 12 ms 3144 KB Output is correct
23 Correct 11 ms 3148 KB Output is correct
24 Correct 10 ms 3144 KB Output is correct
25 Correct 10 ms 3152 KB Output is correct
26 Correct 10 ms 3148 KB Output is correct
27 Correct 11 ms 3140 KB Output is correct
28 Correct 12 ms 3152 KB Output is correct
29 Correct 10 ms 2892 KB Output is correct
30 Correct 9 ms 2916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 3200 KB Output is correct
2 Correct 16 ms 3196 KB Output is correct
3 Correct 16 ms 3700 KB Output is correct
4 Correct 11 ms 3300 KB Output is correct
5 Correct 11 ms 3068 KB Output is correct
6 Correct 11 ms 2808 KB Output is correct
7 Correct 10 ms 2816 KB Output is correct
8 Correct 10 ms 2816 KB Output is correct
9 Correct 10 ms 2812 KB Output is correct
10 Correct 9 ms 3316 KB Output is correct
11 Correct 12 ms 3320 KB Output is correct
12 Correct 10 ms 3200 KB Output is correct
13 Correct 10 ms 3292 KB Output is correct
14 Correct 11 ms 3296 KB Output is correct
15 Correct 11 ms 3312 KB Output is correct
16 Correct 11 ms 3308 KB Output is correct
17 Correct 11 ms 3328 KB Output is correct
18 Correct 11 ms 3320 KB Output is correct
19 Correct 11 ms 3320 KB Output is correct
20 Correct 7 ms 2824 KB Output is correct
21 Correct 7 ms 2824 KB Output is correct
22 Correct 10 ms 2808 KB Output is correct
23 Correct 10 ms 2808 KB Output is correct
24 Correct 12 ms 2812 KB Output is correct
25 Correct 10 ms 2808 KB Output is correct
26 Correct 10 ms 2804 KB Output is correct
27 Correct 11 ms 2816 KB Output is correct
28 Correct 11 ms 2808 KB Output is correct
29 Correct 9 ms 2812 KB Output is correct
30 Correct 9 ms 2812 KB Output is correct
31 Correct 1 ms 784 KB Output is correct
32 Correct 0 ms 796 KB Output is correct
33 Correct 0 ms 804 KB Output is correct
34 Correct 0 ms 780 KB Output is correct
35 Correct 0 ms 784 KB Output is correct
36 Correct 1 ms 800 KB Output is correct
37 Correct 0 ms 788 KB Output is correct
38 Correct 1 ms 800 KB Output is correct
39 Correct 0 ms 788 KB Output is correct
40 Correct 0 ms 800 KB Output is correct
41 Correct 0 ms 784 KB Output is correct
42 Correct 1 ms 800 KB Output is correct