답안 #202384

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
202384 2020-02-16T00:31:59 Z tri Amusement Park (JOI17_amusement_park) C++14
100 / 100
47 ms 5900 KB
#include <bits/stdc++.h>
#include "Joi.h"

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define pb push_back
#define f first
#define s second

namespace debug {
    const int DEBUG = true;

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x);

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x);

    template<class T>
    void pr(const vector<T> &x);

    template<class T>
    void pr(const set<T> &x);

    template<class T1, class T2>
    void pr(const map<T1, T2> &x);

    template<class T>
    void pr(const T &x) { if (DEBUG) cout << x; }

    template<class T, class... Ts>
    void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }

    template<class T>
    void prIn(const T &x) {
        pr("{");
        bool fst = 1;
        for (auto &a : x) {
            pr(fst ? "" : ", ", a), fst = 0;
        }
        pr("}");
    }

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x) { prIn(x); }

    template<class T>
    void pr(const vector<T> &x) { prIn(x); }

    template<class T>
    void pr(const set<T> &x) { prIn(x); }

    template<class T1, class T2>
    void pr(const map<T1, T2> &x) { prIn(x); }

    void ps() { pr("\n"), cout << flush; }

    template<class Arg, class... Args>
    void ps(const Arg &first, const Args &... rest) {
        pr(first, " ");
        ps(rest...);
    }
}
using namespace debug;


namespace shared {
    const int MAXN = 1e4 + 10;

    int N;
    int M;

    vi aL[MAXN];
    vi tL[MAXN];
    int par[MAXN];

    bool vis[MAXN];
    int maxH[MAXN];
    int type[MAXN];

    int mode = -1;

    void dfs(int cV) {
        vis[cV] = true;
        for (int aV : aL[cV]) {
            if (!vis[aV]) {
                dfs(aV);
                tL[cV].pb(aV);
                par[aV] = cV;
            }
        }
    }

    void dfs2(int cV) {
        for (int aV : tL[cV]) {
            dfs2(aV);
            maxH[cV] = max(maxH[cV], maxH[aV] + 1);
        }
    }

    void dfs3(int cV, int cT) {
        type[cV] = cT;
        for (int aV : tL[cV]) {
            dfs3(aV, (cT + 1) % 60);
        }
    }

    vi assigned;
    int nType = 0;

    int dfs4(int cV, int bound, bool lastG) {
        if (bound == 0) return 0;

        type[cV] = nType++;
        assigned.pb(cV);
        bound--;

        if (!lastG) { // if it isn't last, pick randomly
            for (int aV : tL[cV]) {
                bound = dfs4(aV, bound, false);
            }
            assigned.pb(-1);
            return bound;
        }

        assert(lastG);

        int bigC = -1;
        for (int aV : tL[cV]) {
            if (maxH[aV] == maxH[cV] - 1) {
                bigC = aV;
                break;
            }
        }

        if (tL[cV].empty()) {
            assert(bound == 0);
            return 0;
        }
        assert(bigC != -1);

        // we need to reserve at least maxH[bigC] for last
        int avail = bound - maxH[bigC];

        for (int aV : tL[cV]) {
            if (aV != bigC) {
                avail = dfs4(aV, avail, false);
            }
        }

        int fBound = maxH[bigC] + avail; // in case we weren't able to fulfill needs earlier
        int rem = dfs4(bigC, fBound, true);

        assert(rem == 0);
        return 0;
    }

    void prep(int iN, int iM, int A[], int B[]) {
        N = iN, M = iM;

        for (int i = 0; i < M; i++) {
            aL[A[i]].pb(B[i]);
            aL[B[i]].pb(A[i]);
        }

        fill(vis, vis + MAXN, false);
        fill(par, par + MAXN, -1);
        dfs(0);

        fill(maxH, maxH + MAXN, 1);
        dfs2(0);

        fill(type, type + MAXN, -1);

        if (maxH[0] >= 60) {
            mode = 0;
            dfs3(0, 0);
        } else {
            mode = 1;
            dfs4(0, 60, true);
        }
    }
}
using namespace shared;

void Joi(int N, int M, int A[], int B[], ll X, int T) {
    prep(N, M, A, B);

//    ps(assigned);

    int bits[60];

    for (int i = 0; i < 60; i++) {
        bits[i] = ((1ll << i) & X) != 0;
    }

    for (int i = 0; i < N; i++) {
        if (type[i] != -1) {
            MessageBoard(i, bits[type[i]]);
        } else {
            MessageBoard(i, 0); // default 0
        }
    }
}
#include <bits/stdc++.h>
#include "Ioi.h"

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define pb push_back
#define f first
#define s second

namespace shared2 {
    const int MAXN = 1e4 + 10;

    int N;
    int M;

    vi aL[MAXN];
    vi tL[MAXN];
    int par[MAXN];

    bool vis[MAXN];
    int maxH[MAXN];
    int type[MAXN];

    int mode = -1;

    void dfs(int cV) {
        vis[cV] = true;
        for (int aV : aL[cV]) {
            if (!vis[aV]) {
                dfs(aV);
                tL[cV].pb(aV);
                par[aV] = cV;
            }
        }
    }

    void dfs2(int cV) {
        for (int aV : tL[cV]) {
            dfs2(aV);
            maxH[cV] = max(maxH[cV], maxH[aV] + 1);
        }
    }

    void dfs3(int cV, int cT) {
        type[cV] = cT;
        for (int aV : tL[cV]) {
            dfs3(aV, (cT + 1) % 60);
        }
    }

    vi assigned;
    int nType = 0;

    int dfs4(int cV, int bound, bool lastG) {
        if (bound == 0) return 0;

        type[cV] = nType++;
        assigned.pb(cV);
        bound--;

        if (!lastG) { // if it isn't last, pick randomly
            for (int aV : tL[cV]) {
                bound = dfs4(aV, bound, false);
            }
            assigned.pb(-1);
            return bound;
        }

        assert(lastG);

        int bigC = -1;
        for (int aV : tL[cV]) {
            if (maxH[aV] == maxH[cV] - 1) {
                bigC = aV;
                break;
            }
        }

        if (tL[cV].empty()) {
            assert(bound == 0);
            return 0;
        }
        assert(bigC != -1);

        // we need to reserve at least maxH[bigC] for last
        int avail = bound - maxH[bigC];

        for (int aV : tL[cV]) {
            if (aV != bigC) {
                avail = dfs4(aV, avail, false);
            }
        }

        int fBound = maxH[bigC] + avail; // in case we weren't able to fulfill needs earlier
        int rem = dfs4(bigC, fBound, true);
        assert(rem == 0);
        return 0;
    }

    void prep(int iN, int iM, int A[], int B[]) {
        N = iN, M = iM;

        for (int i = 0; i < M; i++) {
            aL[A[i]].pb(B[i]);
            aL[B[i]].pb(A[i]);
        }

        fill(vis, vis + MAXN, false);
        fill(par, par + MAXN, -1);
        dfs(0);

        fill(maxH, maxH + MAXN, 1);
        dfs2(0);

        fill(type, type + MAXN, -1);

        if (maxH[0] >= 60) {
            mode = 0;
            dfs3(0, 0);
        } else {
            mode = 1;
            dfs4(0, 60, true);
        }
    }
}
using namespace shared2;

ll process0(int cV, ll cVal) {
    ll ans = 0;
    ans |= (cVal << type[cV]);

    while (type[cV] != 0) {
        cV = par[cV];
        cVal = Move(cV);
        ans |= (cVal << type[cV]);
    }

    if (cV != 0) {
        cV = par[cV];
        cVal = Move(cV);
        ans |= (cVal << type[cV]);

        while (type[cV] != 0) {
            cV = par[cV];
            cVal = Move(cV);
            ans |= (cVal << type[cV]);
        }

        return ans;
    }

    for (int i = 1; i < 60; i++) {
        for (int aV : tL[cV]) {
            if (maxH[aV] == maxH[cV] - 1) {
                cV = aV;
                cVal = Move(cV);

//                cout << "cVal: " << cVal << endl;
//                cout << "i: " << i << endl;
                ans |= cVal << i;
//                cout << "intAns: " << ans << endl;
                break;
            }
        }
    }
    return ans;
}

ll process1(int cV, ll cVal) {
    while (cV != 0) {
        cV = par[cV];
        cVal = Move(cV);
    }

    ll ans = 0;

    assert(assigned[0] == 0);

    ans |= cVal;

    int i = 1;

    for (int x : assigned) {
        if (x == 0) continue;

        if (x != -1) {
            cV = x;
            cVal = Move(cV);

            ans |= cVal << i;

            i++;
        } else {
            cV = par[cV];
            cVal = Move(cV);
        }
    }
    return ans;
}

ll Ioi(int N, int M, int A[], int B[], int initV, int firstVal, int T) {
    prep(N, M, A, B);

    ll ans;
    if (mode == 0) {
        ans = process0(initV, firstVal);
    } else {
        ans = process1(initV, firstVal);
    }
//    cout << "mode: " << mode << endl;
//    cout << "think: " << ans << endl;
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 1928 KB Output is correct
2 Correct 10 ms 2048 KB Output is correct
3 Correct 11 ms 2140 KB Output is correct
4 Correct 10 ms 2060 KB Output is correct
5 Correct 10 ms 2300 KB Output is correct
6 Correct 10 ms 2028 KB Output is correct
7 Correct 10 ms 2264 KB Output is correct
8 Correct 10 ms 2300 KB Output is correct
9 Correct 11 ms 2148 KB Output is correct
10 Correct 10 ms 2040 KB Output is correct
11 Correct 14 ms 2468 KB Output is correct
12 Correct 10 ms 2168 KB Output is correct
13 Correct 12 ms 2048 KB Output is correct
14 Correct 10 ms 2296 KB Output is correct
15 Correct 10 ms 2304 KB Output is correct
16 Correct 11 ms 2272 KB Output is correct
17 Correct 10 ms 2284 KB Output is correct
18 Correct 10 ms 2304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 5900 KB Output is correct
2 Correct 43 ms 5872 KB Output is correct
3 Correct 40 ms 5864 KB Output is correct
4 Correct 27 ms 4096 KB Output is correct
5 Correct 27 ms 4828 KB Output is correct
6 Correct 27 ms 4492 KB Output is correct
7 Correct 27 ms 4392 KB Output is correct
8 Correct 28 ms 4552 KB Output is correct
9 Correct 27 ms 4544 KB Output is correct
10 Correct 24 ms 3984 KB Output is correct
11 Correct 25 ms 3872 KB Output is correct
12 Correct 27 ms 3748 KB Output is correct
13 Correct 28 ms 3764 KB Output is correct
14 Correct 25 ms 3736 KB Output is correct
15 Correct 27 ms 4356 KB Output is correct
16 Correct 27 ms 4240 KB Output is correct
17 Correct 27 ms 4136 KB Output is correct
18 Correct 27 ms 4124 KB Output is correct
19 Correct 27 ms 3868 KB Output is correct
20 Correct 24 ms 4552 KB Output is correct
21 Correct 24 ms 4568 KB Output is correct
22 Correct 27 ms 4328 KB Output is correct
23 Correct 27 ms 4620 KB Output is correct
24 Correct 27 ms 4256 KB Output is correct
25 Correct 27 ms 4264 KB Output is correct
26 Correct 27 ms 4768 KB Output is correct
27 Correct 28 ms 4508 KB Output is correct
28 Correct 27 ms 4436 KB Output is correct
29 Correct 26 ms 4296 KB Output is correct
30 Correct 28 ms 4300 KB Output is correct
31 Correct 11 ms 1928 KB Output is correct
32 Correct 10 ms 2196 KB Output is correct
33 Correct 10 ms 2432 KB Output is correct
34 Correct 10 ms 1908 KB Output is correct
35 Correct 10 ms 1932 KB Output is correct
36 Correct 10 ms 1932 KB Output is correct
37 Correct 10 ms 1932 KB Output is correct
38 Correct 10 ms 1940 KB Output is correct
39 Correct 10 ms 1952 KB Output is correct
40 Correct 10 ms 1928 KB Output is correct
41 Correct 10 ms 1912 KB Output is correct
42 Correct 10 ms 2060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 1936 KB Output is correct
2 Correct 10 ms 2040 KB Output is correct
3 Correct 10 ms 2160 KB Output is correct
4 Correct 13 ms 2480 KB Output is correct
5 Correct 13 ms 2848 KB Output is correct
6 Correct 12 ms 2468 KB Output is correct
7 Correct 12 ms 2480 KB Output is correct
8 Correct 12 ms 2484 KB Output is correct
9 Correct 27 ms 5620 KB Output is correct
10 Correct 24 ms 5632 KB Output is correct
11 Correct 25 ms 5640 KB Output is correct
12 Correct 10 ms 1936 KB Output is correct
13 Correct 10 ms 2072 KB Output is correct
14 Correct 10 ms 1912 KB Output is correct
15 Correct 10 ms 1912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 5776 KB Output is correct
2 Correct 47 ms 5656 KB Output is correct
3 Correct 41 ms 5868 KB Output is correct
4 Correct 27 ms 4076 KB Output is correct
5 Correct 27 ms 5056 KB Output is correct
6 Correct 28 ms 4536 KB Output is correct
7 Correct 27 ms 4564 KB Output is correct
8 Correct 27 ms 4268 KB Output is correct
9 Correct 27 ms 4524 KB Output is correct
10 Correct 24 ms 4104 KB Output is correct
11 Correct 28 ms 4192 KB Output is correct
12 Correct 25 ms 3760 KB Output is correct
13 Correct 28 ms 3768 KB Output is correct
14 Correct 26 ms 4128 KB Output is correct
15 Correct 28 ms 4232 KB Output is correct
16 Correct 30 ms 4232 KB Output is correct
17 Correct 27 ms 4052 KB Output is correct
18 Correct 28 ms 4052 KB Output is correct
19 Correct 27 ms 4012 KB Output is correct
20 Correct 25 ms 4524 KB Output is correct
21 Correct 23 ms 4576 KB Output is correct
22 Correct 27 ms 4508 KB Output is correct
23 Correct 27 ms 4280 KB Output is correct
24 Correct 27 ms 4268 KB Output is correct
25 Correct 27 ms 4612 KB Output is correct
26 Correct 27 ms 4520 KB Output is correct
27 Correct 27 ms 4588 KB Output is correct
28 Correct 28 ms 4228 KB Output is correct
29 Correct 26 ms 4336 KB Output is correct
30 Correct 27 ms 4256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 5860 KB Output is correct
2 Correct 40 ms 5868 KB Output is correct
3 Correct 40 ms 5888 KB Output is correct
4 Correct 33 ms 4076 KB Output is correct
5 Correct 28 ms 5376 KB Output is correct
6 Correct 28 ms 4508 KB Output is correct
7 Correct 27 ms 4404 KB Output is correct
8 Correct 27 ms 4524 KB Output is correct
9 Correct 28 ms 4600 KB Output is correct
10 Correct 26 ms 3892 KB Output is correct
11 Correct 25 ms 3984 KB Output is correct
12 Correct 25 ms 3700 KB Output is correct
13 Correct 25 ms 3740 KB Output is correct
14 Correct 26 ms 4000 KB Output is correct
15 Correct 27 ms 4204 KB Output is correct
16 Correct 32 ms 4236 KB Output is correct
17 Correct 28 ms 4068 KB Output is correct
18 Correct 27 ms 4056 KB Output is correct
19 Correct 27 ms 4028 KB Output is correct
20 Correct 25 ms 4776 KB Output is correct
21 Correct 24 ms 4564 KB Output is correct
22 Correct 27 ms 4512 KB Output is correct
23 Correct 28 ms 4296 KB Output is correct
24 Correct 32 ms 4512 KB Output is correct
25 Correct 30 ms 4508 KB Output is correct
26 Correct 27 ms 4324 KB Output is correct
27 Correct 27 ms 4576 KB Output is correct
28 Correct 28 ms 4708 KB Output is correct
29 Correct 27 ms 4496 KB Output is correct
30 Correct 27 ms 4380 KB Output is correct
31 Correct 10 ms 1924 KB Output is correct
32 Correct 10 ms 2056 KB Output is correct
33 Correct 10 ms 2304 KB Output is correct
34 Correct 10 ms 1944 KB Output is correct
35 Correct 11 ms 1932 KB Output is correct
36 Correct 10 ms 1936 KB Output is correct
37 Correct 11 ms 1932 KB Output is correct
38 Correct 10 ms 1928 KB Output is correct
39 Correct 10 ms 1912 KB Output is correct
40 Correct 10 ms 1928 KB Output is correct
41 Correct 10 ms 2060 KB Output is correct
42 Correct 11 ms 2056 KB Output is correct