답안 #526056

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
526056 2022-02-13T16:01:06 Z two_sides Cats or Dogs (JOI18_catdog) C++17
100 / 100
244 ms 30240 KB
#include <bits/stdc++.h>

using namespace std;

template <class X, class Y>
bool cmin(X &a, const Y &b) {
    return a > b ? a = b, 1 : 0;
}

const int INF = 1e9;

struct Tp {
    int val[2][2];

    Tp operator + (const Tp &o) const {
        Tp res;
        for (int i = 0; i < 2; i++)
            for (int j = 0; j < 2; j++) {
                res.val[i][j] = min({INF,
                val[i][0] + o.val[1][j] + 1,
                val[i][1] + o.val[0][j] + 1,
                val[i][0] + o.val[0][j],
                val[i][1] + o.val[1][j]});
            }
        return res;
    }
};

struct segTree {
private:
    vector <Tp> tr; int n, lo, hi;

    void build(int i, int l, int r) {
        if (l == r) {
            tr[i].val[0][0] = tr[i].val[1][1] = 0;
            tr[i].val[0][1] = tr[i].val[1][0] = INF;
        }
        else {
            int m = (l + r) / 2;
            build(i * 2, l, m);
            build(i * 2 + 1, m + 1, r);
            tr[i] = tr[i * 2] + tr[i * 2 + 1];
        }
    }

    void update(int i, int l, int r, int v0, int v1) {
        if (l == r) {
            tr[i].val[0][0] += v0;
            tr[i].val[1][1] += v1;
            return;
        }
        int m = (l + r) / 2;
        if (m >= lo) update(i * 2, l, m, v0, v1);
        else update(i * 2 + 1, m + 1, r, v0, v1);
        tr[i] = tr[i * 2] + tr[i * 2 + 1];
    }

public:
    void init(int n) {
        this->n = n; tr.resize(n * 4); build(1, 1, n);
    }

    void update(int p, int v0, int v1) {
        lo = p; update(1, 1, n, v0, v1);
    }

    void query(int &v0, int &v1) {
        int t0 = min(tr[1].val[0][0], tr[1].val[0][1]);
        int t1 = min(tr[1].val[1][0], tr[1].val[1][1]);
        v0 = min(t0, t1 + 1); v1 = min(t0 + 1, t1);
    }
};

const int N = 100005;

vector <int> adj[N]; segTree ST[N];
int hv[N], head[N], par[N], col[N];
int pos[N], grpID[N], grpSz[N], cntGrp;

int DFS1(int u) {
    int siz = 1, maxSiz = 0, tmp;
    for (int v : adj[u]) if (v != par[u]) {
        par[v] = u; siz += tmp = DFS1(v);
        if (maxSiz < tmp) {
            maxSiz = tmp; hv[u] = v;
        }
    }
    return siz;
}

void DFS2(int u) {
    if (u == hv[par[u]]) {
        grpID[u] = grpID[par[u]];
        head[u] = head[par[u]];
    }
    else {
        grpID[u] = ++cntGrp;
        head[u] = u;
    }
    pos[u] = ++grpSz[grpID[u]];
    for (int v : adj[u])
        if (v != par[u]) DFS2(v);
}

int change(int u, int v0, int v1) {
    while (u) {
        int id = grpID[u];
        int cur0, cur1, nxt0, nxt1;
        ST[id].query(cur0, cur1);
        ST[id].update(pos[u], v0, v1);
        ST[id].query(nxt0, nxt1);
        v0 = nxt0 - cur0;
        v1 = nxt1 - cur1;
        u = par[head[u]];
    }
    ST[1].query(v0, v1);
    return min(v0, v1);
}

void initialize(int n, vector<int> a, vector<int> b) {
    fill(col + 1, col + n + 1, 3);
    for (int i = 0; i < n - 1; i++) {
        adj[a[i]].push_back(b[i]);
        adj[b[i]].push_back(a[i]);
    }
    DFS1(1); DFS2(1);
    for (int i = 1; i <= cntGrp; i++)
        ST[i].init(grpSz[i]);
}

int dog(int u) {
    col[u] = 1;
    return change(u, 0, N);
}

int cat(int u) {
    col[u] = 2;
    return change(u, N, 0);
}

int neighbor(int u) {
    int res;
    if (col[u] == 1)
        res = change(u, 0, -N);
    else res = change(u, -N, 0);
    col[u] = 3; return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6476 KB Output is correct
2 Correct 3 ms 6476 KB Output is correct
3 Correct 3 ms 6476 KB Output is correct
4 Correct 4 ms 6476 KB Output is correct
5 Correct 4 ms 6556 KB Output is correct
6 Correct 4 ms 6476 KB Output is correct
7 Correct 4 ms 6476 KB Output is correct
8 Correct 4 ms 6568 KB Output is correct
9 Correct 4 ms 6476 KB Output is correct
10 Correct 3 ms 6476 KB Output is correct
11 Correct 4 ms 6560 KB Output is correct
12 Correct 3 ms 6476 KB Output is correct
13 Correct 4 ms 6556 KB Output is correct
14 Correct 3 ms 6500 KB Output is correct
15 Correct 3 ms 6476 KB Output is correct
16 Correct 3 ms 6476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6476 KB Output is correct
2 Correct 3 ms 6476 KB Output is correct
3 Correct 3 ms 6476 KB Output is correct
4 Correct 4 ms 6476 KB Output is correct
5 Correct 4 ms 6556 KB Output is correct
6 Correct 4 ms 6476 KB Output is correct
7 Correct 4 ms 6476 KB Output is correct
8 Correct 4 ms 6568 KB Output is correct
9 Correct 4 ms 6476 KB Output is correct
10 Correct 3 ms 6476 KB Output is correct
11 Correct 4 ms 6560 KB Output is correct
12 Correct 3 ms 6476 KB Output is correct
13 Correct 4 ms 6556 KB Output is correct
14 Correct 3 ms 6500 KB Output is correct
15 Correct 3 ms 6476 KB Output is correct
16 Correct 3 ms 6476 KB Output is correct
17 Correct 5 ms 6716 KB Output is correct
18 Correct 4 ms 6604 KB Output is correct
19 Correct 4 ms 6604 KB Output is correct
20 Correct 4 ms 6596 KB Output is correct
21 Correct 4 ms 6572 KB Output is correct
22 Correct 4 ms 6604 KB Output is correct
23 Correct 5 ms 6704 KB Output is correct
24 Correct 4 ms 6604 KB Output is correct
25 Correct 4 ms 6604 KB Output is correct
26 Correct 4 ms 6568 KB Output is correct
27 Correct 5 ms 6604 KB Output is correct
28 Correct 4 ms 6732 KB Output is correct
29 Correct 4 ms 6732 KB Output is correct
30 Correct 5 ms 6572 KB Output is correct
31 Correct 4 ms 6604 KB Output is correct
32 Correct 4 ms 6568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6476 KB Output is correct
2 Correct 3 ms 6476 KB Output is correct
3 Correct 3 ms 6476 KB Output is correct
4 Correct 4 ms 6476 KB Output is correct
5 Correct 4 ms 6556 KB Output is correct
6 Correct 4 ms 6476 KB Output is correct
7 Correct 4 ms 6476 KB Output is correct
8 Correct 4 ms 6568 KB Output is correct
9 Correct 4 ms 6476 KB Output is correct
10 Correct 3 ms 6476 KB Output is correct
11 Correct 4 ms 6560 KB Output is correct
12 Correct 3 ms 6476 KB Output is correct
13 Correct 4 ms 6556 KB Output is correct
14 Correct 3 ms 6500 KB Output is correct
15 Correct 3 ms 6476 KB Output is correct
16 Correct 3 ms 6476 KB Output is correct
17 Correct 5 ms 6716 KB Output is correct
18 Correct 4 ms 6604 KB Output is correct
19 Correct 4 ms 6604 KB Output is correct
20 Correct 4 ms 6596 KB Output is correct
21 Correct 4 ms 6572 KB Output is correct
22 Correct 4 ms 6604 KB Output is correct
23 Correct 5 ms 6704 KB Output is correct
24 Correct 4 ms 6604 KB Output is correct
25 Correct 4 ms 6604 KB Output is correct
26 Correct 4 ms 6568 KB Output is correct
27 Correct 5 ms 6604 KB Output is correct
28 Correct 4 ms 6732 KB Output is correct
29 Correct 4 ms 6732 KB Output is correct
30 Correct 5 ms 6572 KB Output is correct
31 Correct 4 ms 6604 KB Output is correct
32 Correct 4 ms 6568 KB Output is correct
33 Correct 108 ms 15580 KB Output is correct
34 Correct 57 ms 16652 KB Output is correct
35 Correct 94 ms 13456 KB Output is correct
36 Correct 168 ms 21904 KB Output is correct
37 Correct 17 ms 11180 KB Output is correct
38 Correct 208 ms 23540 KB Output is correct
39 Correct 244 ms 23576 KB Output is correct
40 Correct 204 ms 23588 KB Output is correct
41 Correct 191 ms 23604 KB Output is correct
42 Correct 203 ms 23600 KB Output is correct
43 Correct 195 ms 23552 KB Output is correct
44 Correct 226 ms 23564 KB Output is correct
45 Correct 210 ms 23560 KB Output is correct
46 Correct 210 ms 23564 KB Output is correct
47 Correct 195 ms 23560 KB Output is correct
48 Correct 83 ms 19336 KB Output is correct
49 Correct 78 ms 22624 KB Output is correct
50 Correct 22 ms 10048 KB Output is correct
51 Correct 30 ms 12844 KB Output is correct
52 Correct 16 ms 9796 KB Output is correct
53 Correct 117 ms 22288 KB Output is correct
54 Correct 65 ms 13440 KB Output is correct
55 Correct 181 ms 18564 KB Output is correct
56 Correct 115 ms 14420 KB Output is correct
57 Correct 164 ms 21400 KB Output is correct
58 Correct 27 ms 13440 KB Output is correct
59 Correct 28 ms 12268 KB Output is correct
60 Correct 75 ms 20992 KB Output is correct
61 Correct 86 ms 21608 KB Output is correct
62 Correct 54 ms 18580 KB Output is correct
63 Correct 41 ms 17204 KB Output is correct
64 Correct 43 ms 19172 KB Output is correct
65 Correct 65 ms 26516 KB Output is correct
66 Correct 49 ms 11640 KB Output is correct
67 Correct 68 ms 20568 KB Output is correct
68 Correct 105 ms 26928 KB Output is correct
69 Correct 41 ms 8392 KB Output is correct
70 Correct 12 ms 6828 KB Output is correct
71 Correct 67 ms 15784 KB Output is correct
72 Correct 79 ms 23336 KB Output is correct
73 Correct 149 ms 30240 KB Output is correct
74 Correct 207 ms 26724 KB Output is correct
75 Correct 116 ms 30100 KB Output is correct
76 Correct 138 ms 28968 KB Output is correct
77 Correct 156 ms 27044 KB Output is correct