답안 #107445

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
107445 2019-04-24T11:24:26 Z szawinis Amusement Park (JOI17_amusement_park) C++17
100 / 100
152 ms 5560 KB
#include "Joi.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4+1;

struct Solver_Joi {
    int n, m, mxd, mxdv, color[N], last[N];
    long long X;
    vector<int> g1[N], g2[N];

    bool vis[N];
    int par[N], depth[N];
    void init_dfs(int u) {
        vis[u] = true;
        if(depth[u] > mxd) {
            mxd = depth[u];
            mxdv = u;
        }
        for(int v: g1[u]) {
            if(vis[v]) continue;
            g2[u].push_back(v);
            par[v] = u;
            depth[v] = depth[u] + 1;
            init_dfs(v);
        }
    }

    void colorBig(int u) {
        color[u] = depth[u] % 60;
        for(int v: g2[u]) {
            colorBig(v);
        }
    }

    int curr_count;
    int st[N];
    vector<int> ord;

    void prepColorSmall1(int u) {
        color[u] = (curr_count >= 60 ? -1 : curr_count);
        ++curr_count;

        if (color[u] != -1) {
            st[u] = ord.size();
            ord.push_back(u);
        }

        for (int v: g2[u]) {
            prepColorSmall1(v);
            if(color[u] != -1 && ord.back() != u) ord.push_back(u);
        }
    }

    void solve() {
        par[0] = -1;
        fill(color, color+N, -1);
        fill(last, last+N, -1);
        init_dfs(0);

        if(mxd >= 59) {
            colorBig(0);
            for(int i = 0; i < n; i++) {
                assert(color[i] != -1 && 0 <= color[i] && color[i] < 60);
                MessageBoard(i, X >> color[i] & 1);
            }
            return;
        }

        prepColorSmall1(0);

        for(int i = 0; i < n; i++) {
            last[i] = i;
            while(color[last[i]] == -1) last[i] = par[last[i]];
        }

        vector<int> sortByDepth;
        for(int i = 0; i < n; i++) sortByDepth.push_back(i);
        sort(sortByDepth.begin(), sortByDepth.end(), [&] (int i, int j) { return depth[i] < depth[j]; });
        for(int i: sortByDepth) {
            if(color[i] != -1) continue;

            int idx = st[last[i]];
            int depth_diff = depth[i] - depth[last[i]];

            set<int> distinct_mods;
            while(distinct_mods.size() < 60 - depth_diff) {
                distinct_mods.insert(color[ord[idx]]);
                idx = (idx + 1) % ord.size();
            }

            for(int v = par[i]; v != last[v]; v = par[v]) {
                assert(color[v] != -1);
                distinct_mods.insert(color[v]);
            }

            for(int mod = 0; mod < 60; mod++) {
                if(!distinct_mods.count(mod)) {
                    color[i] = mod;
                    break;
                }
            }
            assert(ord[st[last[i]]] == last[i]);
        }

        for(int i = 0; i < n; i++) {
            assert(color[i] != -1 && 0 <= color[i] && color[i] < 60);
            MessageBoard(i, X >> color[i] & 1);
        }
    }

    Solver_Joi(int n, int m, long long X, int A[], int B[]): n(n), m(m), X(X) {
        for(int i = 0; i < m; i++) {
            g1[A[i]].push_back(B[i]);
            g1[B[i]].push_back(A[i]);
        }
    }
};

void Joi(int n, int m, int A[], int B[], long long X, int T) {
    Solver_Joi *solver = new Solver_Joi(n, m, X, A, B);
    solver->solve();
}
#include "Ioi.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4+1;

struct Solver_Ioi {
    int n, m, mxd, mxdv, P, V, color[N], last[N];
    long long X;
    vector<int> g1[N], g2[N];

    bool vis[N];
    int par[N], depth[N];

    void init_dfs(int u) {
        vis[u] = true;
        if (depth[u] > mxd) {
            mxd = depth[u];
            mxdv = u;
        }
        for (int v: g1[u]) {
            if (vis[v]) continue;
            g2[u].push_back(v);
            par[v] = u;
            depth[v] = depth[u] + 1;
            init_dfs(v);
        }
    }

    void colorBig(int u) {
        color[u] = depth[u] % 60;
        for (int v: g2[u]) {
            colorBig(v);
        }
    }

    int curr_count;
    int st[N];
    vector<int> ord;

    void prepColorSmall1(int u) {
        color[u] = (curr_count >= 60 ? -1 : curr_count);
        ++curr_count;

        if (color[u] != -1) {
            st[u] = ord.size();
            ord.push_back(u);
        }

        for (int v: g2[u]) {
            prepColorSmall1(v);
            if(color[u] != -1 && ord.back() != u) ord.push_back(u);
        }
    }

    void traverseUp(int v, set<int> &distinct_mods) { // check both cases where v is in top set and bottom set
        assert(v == last[v]);
        int idx = st[v];
        while(distinct_mods.size() < 60) {
            long long tmp = Move(ord[idx]);
            assert(color[ord[idx]] != -1);
            X |= tmp << color[ord[idx]];
            distinct_mods.insert(color[ord[idx]]);
            idx = (idx + 1) % ord.size();
        }
        // this function assumes that v has not been visited yet
    }

    void solve() {
        par[0] = -1;
        fill(color, color + N, -1);
        fill(last, last + N, -1);
        init_dfs(0);

        if (mxd >= 59) {
            set<int> distinct_mods;
            colorBig(0);
            distinct_mods.insert(color[P]);
            X |= 1ll * V << color[P];
            for(int v = par[P]; v >= 0 && distinct_mods.size() < 60; v = par[v]) {
                long long tmp = Move(v);
                distinct_mods.insert(color[v]);
                X |= tmp << color[v];
            }
            vector<int> ord;
            for(int v = mxdv; v > 0; v = par[v]) ord.push_back(v);
            reverse(ord.begin(), ord.end());
            for(int v: ord) {
                if(distinct_mods.size() >= 60) break;
                long long tmp = Move(v);
                distinct_mods.insert(color[v]);
                X |= tmp << color[v];
            }
            return;
        }

        prepColorSmall1(0);
        ord.pop_back();

        for (int i = 0; i < n; i++) {
            last[i] = i;
            while (color[last[i]] == -1) last[i] = par[last[i]];
        }

        vector<int> sortByDepth;
        for(int i = 0; i < n; i++) sortByDepth.push_back(i);
        sort(sortByDepth.begin(), sortByDepth.end(), [&] (int i, int j) { return depth[i] < depth[j]; });
        for(int i: sortByDepth) {
            if(color[i] != -1) continue;

            int idx = st[last[i]];
            int depth_diff = depth[i] - depth[last[i]];

            set<int> distinct_mods;
            while(distinct_mods.size() < 60 - depth_diff) {
                distinct_mods.insert(color[ord[idx]]);
                idx = (idx + 1) % ord.size();
            }

            for(int v = par[i]; v != last[v]; v = par[v]) { // assures that there are no duplicate mods on the way up. Crucial for last subtask
                assert(color[v] != -1);
                distinct_mods.insert(color[v]);
            }

            for(int mod = 0; mod < 60; mod++) {
                if(!distinct_mods.count(mod)) {
                    color[i] = mod;
                    break;
                }
            }
            assert(ord[st[last[i]]] == last[i]);
        }

        set<int> distinct_mods; // keep the set of all colors visited so far
        distinct_mods.insert(color[P]);
        X |= 1ll * V << color[P];

        if(last[P] == P) {
            int nxt = ord[(st[P] + 1) % ord.size()];
            assert(last[nxt] == nxt);
            traverseUp(nxt, distinct_mods);
        } else {
            int v;
            for(v = par[P]; last[v] != v; v = par[v]) {
                assert(last[v] == last[P]);
                long long tmp = Move(v);
                distinct_mods.insert(color[v]);
                X |= tmp << color[v];
            }
            traverseUp(v, distinct_mods);
        }
    }

    Solver_Ioi(int n, int m, int P, int V, int A[], int B[]) : n(n), m(m), P(P), V(V) {
        for (int i = 0; i < m; i++) {
            g1[A[i]].push_back(B[i]);
            g1[B[i]].push_back(A[i]);
        }
    }
};

long long Ioi(int n, int m, int A[], int B[], int P, int V, int T) {
    Solver_Ioi *solverIoi = new Solver_Ioi(n, m, P, V, A, B);
    solverIoi->solve();
    return solverIoi->X;
}

Compilation message

Joi.cpp: In member function 'void Solver_Joi::solve()':
Joi.cpp:86:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(distinct_mods.size() < 60 - depth_diff) {
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~

Ioi.cpp: In member function 'void Solver_Ioi::solve()':
Ioi.cpp:114:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(distinct_mods.size() < 60 - depth_diff) {
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1932 KB Output is correct
2 Correct 6 ms 1940 KB Output is correct
3 Correct 8 ms 2300 KB Output is correct
4 Correct 4 ms 1792 KB Output is correct
5 Correct 5 ms 1972 KB Output is correct
6 Correct 7 ms 1928 KB Output is correct
7 Correct 6 ms 1924 KB Output is correct
8 Correct 7 ms 2052 KB Output is correct
9 Correct 7 ms 2200 KB Output is correct
10 Correct 6 ms 1920 KB Output is correct
11 Correct 9 ms 2360 KB Output is correct
12 Correct 4 ms 1920 KB Output is correct
13 Correct 8 ms 2180 KB Output is correct
14 Correct 8 ms 2320 KB Output is correct
15 Correct 9 ms 2168 KB Output is correct
16 Correct 9 ms 2172 KB Output is correct
17 Correct 9 ms 2304 KB Output is correct
18 Correct 9 ms 1924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 5204 KB Output is correct
2 Correct 35 ms 5412 KB Output is correct
3 Correct 30 ms 5460 KB Output is correct
4 Correct 119 ms 4260 KB Output is correct
5 Correct 19 ms 4668 KB Output is correct
6 Correct 19 ms 4296 KB Output is correct
7 Correct 19 ms 4296 KB Output is correct
8 Correct 19 ms 4528 KB Output is correct
9 Correct 24 ms 4524 KB Output is correct
10 Correct 106 ms 4032 KB Output is correct
11 Correct 114 ms 4088 KB Output is correct
12 Correct 107 ms 3944 KB Output is correct
13 Correct 100 ms 3936 KB Output is correct
14 Correct 103 ms 4068 KB Output is correct
15 Correct 121 ms 4316 KB Output is correct
16 Correct 110 ms 4404 KB Output is correct
17 Correct 121 ms 4288 KB Output is correct
18 Correct 124 ms 4252 KB Output is correct
19 Correct 129 ms 4164 KB Output is correct
20 Correct 20 ms 4424 KB Output is correct
21 Correct 18 ms 4536 KB Output is correct
22 Correct 19 ms 4168 KB Output is correct
23 Correct 26 ms 4320 KB Output is correct
24 Correct 23 ms 4276 KB Output is correct
25 Correct 26 ms 4220 KB Output is correct
26 Correct 20 ms 4288 KB Output is correct
27 Correct 20 ms 4296 KB Output is correct
28 Correct 26 ms 4288 KB Output is correct
29 Correct 27 ms 4016 KB Output is correct
30 Correct 20 ms 4256 KB Output is correct
31 Correct 7 ms 2056 KB Output is correct
32 Correct 6 ms 1792 KB Output is correct
33 Correct 8 ms 2088 KB Output is correct
34 Correct 7 ms 1920 KB Output is correct
35 Correct 6 ms 2048 KB Output is correct
36 Correct 5 ms 1792 KB Output is correct
37 Correct 5 ms 2048 KB Output is correct
38 Correct 4 ms 1940 KB Output is correct
39 Correct 4 ms 1968 KB Output is correct
40 Correct 5 ms 1928 KB Output is correct
41 Correct 7 ms 1940 KB Output is correct
42 Correct 6 ms 1928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1956 KB Output is correct
2 Correct 5 ms 1920 KB Output is correct
3 Correct 4 ms 1792 KB Output is correct
4 Correct 8 ms 2368 KB Output is correct
5 Correct 8 ms 2516 KB Output is correct
6 Correct 8 ms 2484 KB Output is correct
7 Correct 8 ms 2484 KB Output is correct
8 Correct 9 ms 2468 KB Output is correct
9 Correct 15 ms 5320 KB Output is correct
10 Correct 16 ms 5320 KB Output is correct
11 Correct 18 ms 5312 KB Output is correct
12 Correct 5 ms 1940 KB Output is correct
13 Correct 5 ms 2048 KB Output is correct
14 Correct 6 ms 1920 KB Output is correct
15 Correct 6 ms 1920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 5332 KB Output is correct
2 Correct 33 ms 5560 KB Output is correct
3 Correct 33 ms 5440 KB Output is correct
4 Correct 124 ms 4220 KB Output is correct
5 Correct 23 ms 4800 KB Output is correct
6 Correct 20 ms 4424 KB Output is correct
7 Correct 22 ms 4536 KB Output is correct
8 Correct 20 ms 4160 KB Output is correct
9 Correct 34 ms 4456 KB Output is correct
10 Correct 125 ms 3896 KB Output is correct
11 Correct 122 ms 4080 KB Output is correct
12 Correct 106 ms 3840 KB Output is correct
13 Correct 104 ms 3920 KB Output is correct
14 Correct 120 ms 3888 KB Output is correct
15 Correct 131 ms 4280 KB Output is correct
16 Correct 130 ms 4744 KB Output is correct
17 Correct 133 ms 4160 KB Output is correct
18 Correct 126 ms 4152 KB Output is correct
19 Correct 150 ms 4152 KB Output is correct
20 Correct 17 ms 4424 KB Output is correct
21 Correct 17 ms 4532 KB Output is correct
22 Correct 20 ms 4168 KB Output is correct
23 Correct 19 ms 4228 KB Output is correct
24 Correct 20 ms 4364 KB Output is correct
25 Correct 20 ms 4288 KB Output is correct
26 Correct 23 ms 4288 KB Output is correct
27 Correct 20 ms 4424 KB Output is correct
28 Correct 31 ms 4032 KB Output is correct
29 Correct 22 ms 4016 KB Output is correct
30 Correct 19 ms 4280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 5332 KB Output is correct
2 Correct 41 ms 5448 KB Output is correct
3 Correct 30 ms 5204 KB Output is correct
4 Correct 121 ms 4216 KB Output is correct
5 Correct 22 ms 5064 KB Output is correct
6 Correct 20 ms 4296 KB Output is correct
7 Correct 24 ms 4224 KB Output is correct
8 Correct 22 ms 4396 KB Output is correct
9 Correct 20 ms 4176 KB Output is correct
10 Correct 114 ms 3904 KB Output is correct
11 Correct 117 ms 4076 KB Output is correct
12 Correct 111 ms 3888 KB Output is correct
13 Correct 98 ms 3956 KB Output is correct
14 Correct 122 ms 4144 KB Output is correct
15 Correct 129 ms 4408 KB Output is correct
16 Correct 152 ms 4452 KB Output is correct
17 Correct 130 ms 4272 KB Output is correct
18 Correct 135 ms 4152 KB Output is correct
19 Correct 139 ms 4296 KB Output is correct
20 Correct 20 ms 4400 KB Output is correct
21 Correct 16 ms 4424 KB Output is correct
22 Correct 28 ms 4160 KB Output is correct
23 Correct 24 ms 4244 KB Output is correct
24 Correct 26 ms 4168 KB Output is correct
25 Correct 27 ms 4380 KB Output is correct
26 Correct 28 ms 4180 KB Output is correct
27 Correct 28 ms 4356 KB Output is correct
28 Correct 26 ms 4636 KB Output is correct
29 Correct 23 ms 4280 KB Output is correct
30 Correct 27 ms 4160 KB Output is correct
31 Correct 7 ms 2048 KB Output is correct
32 Correct 6 ms 1932 KB Output is correct
33 Correct 8 ms 2188 KB Output is correct
34 Correct 6 ms 1928 KB Output is correct
35 Correct 5 ms 1948 KB Output is correct
36 Correct 5 ms 1940 KB Output is correct
37 Correct 5 ms 1792 KB Output is correct
38 Correct 6 ms 1920 KB Output is correct
39 Correct 7 ms 1940 KB Output is correct
40 Correct 6 ms 1928 KB Output is correct
41 Correct 6 ms 1940 KB Output is correct
42 Correct 7 ms 1972 KB Output is correct