Submission #202458

# Submission time Handle Problem Language Result Execution time Memory
202458 2020-02-16T07:47:13 Z tri Cats or Dogs (JOI18_catdog) C++14
100 / 100
809 ms 23008 KB
#include <bits/stdc++.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 = false;

    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;

const int MAXN = 1e5 + 100;
const int INF = 1e8;

struct node {
    int c[2][2] = {{0,   INF},
                   {INF, 0}}; // empty node can only stay the same type
};

node infNode() {
    node ans;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            ans.c[i][j] = INF;
        }
    }
    return ans;
}

node nullNode() {
    node ans;
    ans.c[0][0] = -1;
    return ans;
}

void ps(node x) {
    ps(x.c[0][0], x.c[0][1], x.c[1][0], x.c[1][1]);
}

const node operator+(node a, node b) {
    if (a.c[0][0] == -1) {
        return b;
    }
    if (b.c[0][0] == -1) {
        return a;
    }

    node ans = infNode();
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                ans.c[i][j] = min(ans.c[i][j], a.c[i][k] + b.c[k][j]);
                ans.c[i][j] = min(ans.c[i][j], a.c[i][k] + b.c[k ^ 1][j] + 1);
            }
        }
    }
    return ans;
}

struct SegTr {
    node tr[4 * MAXN];

    void b(int i, int l, int r) {
        if (l == r) {
            return;
        }
        int mid = (l + r) / 2;
        b(i * 2, l, mid);
        b(i * 2 + 1, mid + 1, r);
        tr[i] = tr[i * 2] + tr[i * 2 + 1];
    }

    node q(int i, int l, int r, int s, int e) {
        if (e < l || r < s) {
            return nullNode();
        }
        if (s <= l && r <= e) {
            return tr[i];
        }
        int mid = (l + r) / 2;
        return q(i * 2, l, mid, s, e) + q(i * 2 + 1, mid + 1, r, s, e);
    }

    void u(int i, int l, int r, int x, node d) {
        if (l == r) {
            tr[i] = d;
            return;
        }
        int mid = (l + r) / 2;
        if (x <= mid) {
            u(i * 2, l, mid, x, d);
        } else {
            u(i * 2 + 1, mid + 1, r, x, d);
        }
        tr[i] = tr[i * 2] + tr[i * 2 + 1];
    }

    node q(int l, int r) {
        return q(1, 0, MAXN - 1, l, r);
    }

    void u(int x, node d) {
        u(1, 0, MAXN - 1, x, d);
    }
};

int N, rt;

vi aL[MAXN];

int p[MAXN], d[MAXN], hC[MAXN], cS[MAXN], cE[MAXN];

SegTr tr;
int tI[MAXN];

int initDFS(int cV) {
    int cCnt = 1, mSCnt = 0;

    for (int aV : aL[cV]) {
        if (aV != p[cV]) {
            p[aV] = cV;
            d[aV] = d[cV] + 1;

            int aCnt = initDFS(aV);
            if (aCnt > mSCnt) {
                mSCnt = aCnt;
                hC[cV] = aV;
            }
            cCnt += aCnt;
        }
    }
    return cCnt;
}

void init() {
    fill(hC, hC + MAXN, -1);
    p[rt] = -1;
    initDFS(rt);

    int nTI = 0;
    for (int curS = 0; curS < N; curS++) {
        if (curS == rt || hC[p[curS]] != curS) {
            for (int cV = curS; cV != -1; cV = hC[cV]) {
                cS[cV] = curS;
                cE[curS] = cV;
                tI[cV] = nTI++;
            }
        }
    }
}

// stores the contributions of all cS to their parents
int contrib[MAXN][2];


// purpose of this function is to edit data structure
void update(int cV, int type, int dCost) {
    ps("updating", cV, type, dCost);
    node edit = tr.q(tI[cV], tI[cV]);
    ps(edit);

    edit.c[type][type] += dCost;

    ps(edit);
    tr.u(tI[cV], edit);

    ps(tr.q(tI[cV], tI[cV]));

    while (cV != -1) {
        int cCS = cS[cV];
        int cCE = cE[cCS];

        node newRes = tr.q(tI[cCS], tI[cCE]);
        ps("start stop", cCS, cCE);

        // end of chain can be anything, only the start is significant
        int startRes[] = {min(newRes.c[0][0], newRes.c[0][1]), min(newRes.c[1][0], newRes.c[1][1])};

        // costs 1 extra to use opposite type
        int newContrib[] = {min(startRes[0], startRes[1] + 1), min(startRes[0] + 1, startRes[1])};

        int cP = p[cCS];
        if (cP != -1) {
            edit = tr.q(tI[cP], tI[cP]);

            ps("inside");
            ps(edit);

            for (int a = 0; a < 2; a++) {
                for (int b = 0; b < 2; b++) {
                    edit.c[a][b] -= contrib[cCS][b];

                    edit.c[a][b] += newContrib[b];
                }
            }

            ps(edit);

            tr.u(tI[cP], edit);
        }

        contrib[cCS][0] = newContrib[0];
        contrib[cCS][1] = newContrib[1];

        cV = cP;
    }
}

int type[MAXN];

//int getAns() {
//    node fRes = tr.q(tI[0], tI[cE[0]]);
//    return min(fRes.c[0][0], fRes.c[0][1]);
//}

int cat(int cV) {
    cV--;
    ps("cat");
    type[cV] = 0;
    update(cV, 1, INF); // force vertex to be cat by making dog cost really large
    return min(contrib[0][0], contrib[0][1]);
}

int dog(int cV) {
    cV--;
    ps("dog");
    type[cV] = 1;
    update(cV, 0, INF);
    return min(contrib[0][0], contrib[0][1]);
}

int neighbor(int cV) {
    cV--;
    ps("unset");
    update(cV, type[cV] ^ 1, -INF); // undo changes to opposite type
    type[cV] = 0;
    return min(contrib[0][0], contrib[0][1]);
}

void initialize(int iN, vi A, vi B) {
    N = iN;
    for (int i = 0; i < N - 1; i++) {
        A[i]--;
        B[i]--;
        aL[A[i]].pb(B[i]);
        aL[B[i]].pb(A[i]);
    }

    rt = 0;
    init();

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

    memset(contrib, 0, sizeof(contrib));

    tr.b(1, 0, MAXN - 1);

//    ps(tr.q(1, 1));
}

void end() {
    node a = tr.q(tI[0], tI[0]);
    node b = tr.q(tI[3], tI[3]);
    node c = tr.q(tI[4], tI[4]);

    ps(a);
    ps(b);
    ps(c);
    ps(b + c);
    ps(a + (b + c));
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 6264 KB Output is correct
2 Correct 11 ms 6392 KB Output is correct
3 Correct 12 ms 6268 KB Output is correct
4 Correct 12 ms 6396 KB Output is correct
5 Correct 11 ms 6392 KB Output is correct
6 Correct 12 ms 6264 KB Output is correct
7 Correct 12 ms 6392 KB Output is correct
8 Correct 11 ms 6264 KB Output is correct
9 Correct 14 ms 6264 KB Output is correct
10 Correct 11 ms 6264 KB Output is correct
11 Correct 14 ms 6396 KB Output is correct
12 Correct 11 ms 6264 KB Output is correct
13 Correct 12 ms 6392 KB Output is correct
14 Correct 11 ms 6264 KB Output is correct
15 Correct 12 ms 6264 KB Output is correct
16 Correct 11 ms 6264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 6264 KB Output is correct
2 Correct 11 ms 6392 KB Output is correct
3 Correct 12 ms 6268 KB Output is correct
4 Correct 12 ms 6396 KB Output is correct
5 Correct 11 ms 6392 KB Output is correct
6 Correct 12 ms 6264 KB Output is correct
7 Correct 12 ms 6392 KB Output is correct
8 Correct 11 ms 6264 KB Output is correct
9 Correct 14 ms 6264 KB Output is correct
10 Correct 11 ms 6264 KB Output is correct
11 Correct 14 ms 6396 KB Output is correct
12 Correct 11 ms 6264 KB Output is correct
13 Correct 12 ms 6392 KB Output is correct
14 Correct 11 ms 6264 KB Output is correct
15 Correct 12 ms 6264 KB Output is correct
16 Correct 11 ms 6264 KB Output is correct
17 Correct 14 ms 6396 KB Output is correct
18 Correct 15 ms 6392 KB Output is correct
19 Correct 13 ms 6392 KB Output is correct
20 Correct 11 ms 6392 KB Output is correct
21 Correct 13 ms 6392 KB Output is correct
22 Correct 12 ms 6392 KB Output is correct
23 Correct 15 ms 6392 KB Output is correct
24 Correct 14 ms 6392 KB Output is correct
25 Correct 14 ms 6392 KB Output is correct
26 Correct 13 ms 6396 KB Output is correct
27 Correct 12 ms 6396 KB Output is correct
28 Correct 12 ms 6392 KB Output is correct
29 Correct 14 ms 6392 KB Output is correct
30 Correct 13 ms 6392 KB Output is correct
31 Correct 12 ms 6392 KB Output is correct
32 Correct 13 ms 6392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 6264 KB Output is correct
2 Correct 11 ms 6392 KB Output is correct
3 Correct 12 ms 6268 KB Output is correct
4 Correct 12 ms 6396 KB Output is correct
5 Correct 11 ms 6392 KB Output is correct
6 Correct 12 ms 6264 KB Output is correct
7 Correct 12 ms 6392 KB Output is correct
8 Correct 11 ms 6264 KB Output is correct
9 Correct 14 ms 6264 KB Output is correct
10 Correct 11 ms 6264 KB Output is correct
11 Correct 14 ms 6396 KB Output is correct
12 Correct 11 ms 6264 KB Output is correct
13 Correct 12 ms 6392 KB Output is correct
14 Correct 11 ms 6264 KB Output is correct
15 Correct 12 ms 6264 KB Output is correct
16 Correct 11 ms 6264 KB Output is correct
17 Correct 14 ms 6396 KB Output is correct
18 Correct 15 ms 6392 KB Output is correct
19 Correct 13 ms 6392 KB Output is correct
20 Correct 11 ms 6392 KB Output is correct
21 Correct 13 ms 6392 KB Output is correct
22 Correct 12 ms 6392 KB Output is correct
23 Correct 15 ms 6392 KB Output is correct
24 Correct 14 ms 6392 KB Output is correct
25 Correct 14 ms 6392 KB Output is correct
26 Correct 13 ms 6396 KB Output is correct
27 Correct 12 ms 6396 KB Output is correct
28 Correct 12 ms 6392 KB Output is correct
29 Correct 14 ms 6392 KB Output is correct
30 Correct 13 ms 6392 KB Output is correct
31 Correct 12 ms 6392 KB Output is correct
32 Correct 13 ms 6392 KB Output is correct
33 Correct 509 ms 12764 KB Output is correct
34 Correct 157 ms 12660 KB Output is correct
35 Correct 437 ms 11504 KB Output is correct
36 Correct 716 ms 16956 KB Output is correct
37 Correct 30 ms 9208 KB Output is correct
38 Correct 809 ms 18020 KB Output is correct
39 Correct 804 ms 18108 KB Output is correct
40 Correct 768 ms 18024 KB Output is correct
41 Correct 784 ms 18148 KB Output is correct
42 Correct 742 ms 18244 KB Output is correct
43 Correct 770 ms 18020 KB Output is correct
44 Correct 742 ms 18024 KB Output is correct
45 Correct 745 ms 17968 KB Output is correct
46 Correct 733 ms 18020 KB Output is correct
47 Correct 786 ms 17960 KB Output is correct
48 Correct 245 ms 14780 KB Output is correct
49 Correct 267 ms 16804 KB Output is correct
50 Correct 116 ms 8952 KB Output is correct
51 Correct 109 ms 10436 KB Output is correct
52 Correct 50 ms 8440 KB Output is correct
53 Correct 333 ms 16368 KB Output is correct
54 Correct 259 ms 11056 KB Output is correct
55 Correct 651 ms 14960 KB Output is correct
56 Correct 413 ms 11900 KB Output is correct
57 Correct 532 ms 15500 KB Output is correct
58 Correct 47 ms 10412 KB Output is correct
59 Correct 106 ms 10232 KB Output is correct
60 Correct 235 ms 15608 KB Output is correct
61 Correct 252 ms 16100 KB Output is correct
62 Correct 145 ms 13904 KB Output is correct
63 Correct 75 ms 13176 KB Output is correct
64 Correct 77 ms 14840 KB Output is correct
65 Correct 95 ms 19384 KB Output is correct
66 Correct 131 ms 10232 KB Output is correct
67 Correct 106 ms 15740 KB Output is correct
68 Correct 212 ms 19704 KB Output is correct
69 Correct 67 ms 7928 KB Output is correct
70 Correct 25 ms 6648 KB Output is correct
71 Correct 106 ms 12792 KB Output is correct
72 Correct 135 ms 18040 KB Output is correct
73 Correct 352 ms 23008 KB Output is correct
74 Correct 357 ms 19448 KB Output is correct
75 Correct 237 ms 22904 KB Output is correct
76 Correct 241 ms 21624 KB Output is correct
77 Correct 357 ms 19960 KB Output is correct