답안 #102586

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
102586 2019-03-26T03:20:40 Z minhtung0404 Cats or Dogs (JOI18_catdog) C++17
100 / 100
1465 ms 19192 KB
#include "catdog.h"
#include<bits/stdc++.h>
const int N = 1e5 + 5;
using namespace std;

void up(int&a, int b){
    if (a > b) a = b;
}

struct nodeIT{
    int a[2][2];

    int Min(int x){
        int ans = a[0][0] + (x == 0);
        for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) up(ans, a[i][j] + (x == i));
        return ans;
    }

//    void out(){
//        for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) cout << a[i][j] << " "; cout << "\n";
//    }
} it[N << 2], zero;

typedef vector <int> vi;
vi G[N];
int n, num[N], p[N], chainidx[N], chainst[N], chainen[N], nchain;
int node[N], pos[N], cnt, dp[N][2];
int ani[N];

nodeIT Merge(nodeIT le, nodeIT ri){
    nodeIT ans;
    for (int i = 0; i < 2; i++){
        for (int j = 0; j < 2; j++){
            ans.a[i][j] = N;
            for (int k = 0; k < 2; k++){
                for (int h = 0; h < 2; h++){
                    up(ans.a[i][j], le.a[i][k] + ri.a[h][j] + (k != h));
                }
            }
        }
    }
    return ans;
}

void update(int i, int l, int r, int pos, int v1, int v2){
    if (l > pos || pos > r) return;
    if (l == r){
        it[i].a[0][0] = v1;
        it[i].a[1][1] = v2;
        it[i].a[0][1] = it[i].a[1][0] = N;
        return;
    }
    int mid = (l + r) >> 1;
    update(i << 1, l, mid, pos, v1, v2); update(i << 1 | 1, mid+1, r, pos, v1, v2);
    it[i] = Merge(it[i << 1], it[i << 1 | 1]);
//    cout << "st " << i << "\n";
//    it[i].out(); it[i << 1].out(); it[i << 1 | 1].out();
//    cout << "en " << i << "\n\n";
}

void init(int i, int l, int r){
    if (l == r){
        it[i] = zero;
        return;
    }
    int mid = (l + r) >> 1;
    init(i << 1, l, mid); init(i << 1 | 1, mid+1, r);
    it[i] = zero;
}

nodeIT get(int i, int l, int r, int L, int R){
    if (L > r || l > R) return zero;
    if (L <= l && r <= R) return it[i];
    int mid = (l + r) >> 1;
    return Merge(get(i << 1, l, mid, L, R), get(i << 1 | 1, mid+1, r, L, R));
}

void dfs(int u){
    num[u] = 1;
    for (auto v : G[u]){
        if (v == p[u]) continue;
        p[v] = u;
        dfs(v);
        num[u] += num[v];
    }
}

void hld(int u){
    pos[u] = ++cnt; node[cnt] = u;
    chainidx[u] = nchain;
    int Max = -1;
    for (auto v : G[u]){
        if (v == p[u]) continue;
        if (Max == -1 || num[Max] < num[v]) Max = v;
    }
    if (Max != -1) hld(Max);
    else chainen[nchain] = u;
    for (auto v : G[u]){
        if (v == p[u] || v == Max) continue;
        chainst[++nchain] = v;
        hld(v);
    }
}

void initialize(int N, vi A, vi B) {
    n = N;
    for (int i = 1; i <= n; i++) ani[i] = 2;
    for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) zero.a[i][j] = (i == j ? 0 : N);
    for (int i = 0; i < n-1; i++){
        G[A[i]].push_back(B[i]);
        G[B[i]].push_back(A[i]);
    }
    dfs(1);
    chainst[++nchain] = 1;
    hld(1); init(1, 1, n);
}

int cat(int v) {
    nodeIT cur; ani[v] = 0;
    while (v){
        int idx = chainidx[v], nxt = p[chainst[idx]];

        cur = get(1, 1, n, pos[chainst[idx]], pos[chainen[idx]]);
        dp[nxt][0] -= cur.Min(1); dp[nxt][1] -= cur.Min(0);

        update(1, 1, n, pos[v], ((ani[v] == 1) ? N : dp[v][0]), ((ani[v] == 0) ? N : dp[v][1]));

        cur = get(1, 1, n, pos[chainst[idx]], pos[chainen[idx]]);
        dp[nxt][0] += cur.Min(1); dp[nxt][1] += cur.Min(0);

        v = nxt;
    }
    return min(dp[0][0], dp[0][1]);
}

int dog(int v) {
    nodeIT cur; ani[v] = 1;
    while (v){
        int idx = chainidx[v], nxt = p[chainst[idx]];

        cur = get(1, 1, n, pos[chainst[idx]], pos[chainen[idx]]);
        dp[nxt][0] -= cur.Min(1); dp[nxt][1] -= cur.Min(0);

        update(1, 1, n, pos[v], ((ani[v] == 1) ? N : dp[v][0]), ((ani[v] == 0) ? N : dp[v][1]));

        cur = get(1, 1, n, pos[chainst[idx]], pos[chainen[idx]]);
        dp[nxt][0] += cur.Min(1); dp[nxt][1] += cur.Min(0);

        v = nxt;
    }
    return min(dp[0][0], dp[0][1]);
}

int neighbor(int v) {
    nodeIT cur; ani[v] = 2;
    while (v){
        int idx = chainidx[v], nxt = p[chainst[idx]];

        cur = get(1, 1, n, pos[chainst[idx]], pos[chainen[idx]]);
        dp[nxt][0] -= cur.Min(1); dp[nxt][1] -= cur.Min(0);

        update(1, 1, n, pos[v], ((ani[v] == 1) ? N : dp[v][0]), ((ani[v] == 0) ? N : dp[v][1]));

        cur = get(1, 1, n, pos[chainst[idx]], pos[chainen[idx]]);
        dp[nxt][0] += cur.Min(1); dp[nxt][1] += cur.Min(0);

        v = nxt;
    }
    return min(dp[0][0], dp[0][1]);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 5 ms 2688 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 5 ms 2688 KB Output is correct
7 Correct 4 ms 2688 KB Output is correct
8 Correct 4 ms 2688 KB Output is correct
9 Correct 5 ms 2688 KB Output is correct
10 Correct 4 ms 2688 KB Output is correct
11 Correct 5 ms 2688 KB Output is correct
12 Correct 5 ms 2688 KB Output is correct
13 Correct 4 ms 2688 KB Output is correct
14 Correct 5 ms 2688 KB Output is correct
15 Correct 5 ms 2688 KB Output is correct
16 Correct 5 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 5 ms 2688 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 5 ms 2688 KB Output is correct
7 Correct 4 ms 2688 KB Output is correct
8 Correct 4 ms 2688 KB Output is correct
9 Correct 5 ms 2688 KB Output is correct
10 Correct 4 ms 2688 KB Output is correct
11 Correct 5 ms 2688 KB Output is correct
12 Correct 5 ms 2688 KB Output is correct
13 Correct 4 ms 2688 KB Output is correct
14 Correct 5 ms 2688 KB Output is correct
15 Correct 5 ms 2688 KB Output is correct
16 Correct 5 ms 2688 KB Output is correct
17 Correct 7 ms 2816 KB Output is correct
18 Correct 7 ms 2816 KB Output is correct
19 Correct 6 ms 2816 KB Output is correct
20 Correct 4 ms 2688 KB Output is correct
21 Correct 6 ms 2944 KB Output is correct
22 Correct 5 ms 2816 KB Output is correct
23 Correct 9 ms 2944 KB Output is correct
24 Correct 8 ms 2816 KB Output is correct
25 Correct 7 ms 2944 KB Output is correct
26 Correct 6 ms 2816 KB Output is correct
27 Correct 6 ms 2816 KB Output is correct
28 Correct 5 ms 2816 KB Output is correct
29 Correct 6 ms 2816 KB Output is correct
30 Correct 7 ms 2688 KB Output is correct
31 Correct 4 ms 2816 KB Output is correct
32 Correct 6 ms 2816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 5 ms 2688 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 5 ms 2688 KB Output is correct
7 Correct 4 ms 2688 KB Output is correct
8 Correct 4 ms 2688 KB Output is correct
9 Correct 5 ms 2688 KB Output is correct
10 Correct 4 ms 2688 KB Output is correct
11 Correct 5 ms 2688 KB Output is correct
12 Correct 5 ms 2688 KB Output is correct
13 Correct 4 ms 2688 KB Output is correct
14 Correct 5 ms 2688 KB Output is correct
15 Correct 5 ms 2688 KB Output is correct
16 Correct 5 ms 2688 KB Output is correct
17 Correct 7 ms 2816 KB Output is correct
18 Correct 7 ms 2816 KB Output is correct
19 Correct 6 ms 2816 KB Output is correct
20 Correct 4 ms 2688 KB Output is correct
21 Correct 6 ms 2944 KB Output is correct
22 Correct 5 ms 2816 KB Output is correct
23 Correct 9 ms 2944 KB Output is correct
24 Correct 8 ms 2816 KB Output is correct
25 Correct 7 ms 2944 KB Output is correct
26 Correct 6 ms 2816 KB Output is correct
27 Correct 6 ms 2816 KB Output is correct
28 Correct 5 ms 2816 KB Output is correct
29 Correct 6 ms 2816 KB Output is correct
30 Correct 7 ms 2688 KB Output is correct
31 Correct 4 ms 2816 KB Output is correct
32 Correct 6 ms 2816 KB Output is correct
33 Correct 810 ms 9712 KB Output is correct
34 Correct 238 ms 9848 KB Output is correct
35 Correct 826 ms 8308 KB Output is correct
36 Correct 1252 ms 15368 KB Output is correct
37 Correct 25 ms 6272 KB Output is correct
38 Correct 1420 ms 15944 KB Output is correct
39 Correct 1457 ms 16056 KB Output is correct
40 Correct 1457 ms 15952 KB Output is correct
41 Correct 1337 ms 15944 KB Output is correct
42 Correct 1293 ms 15980 KB Output is correct
43 Correct 1465 ms 15976 KB Output is correct
44 Correct 1420 ms 16104 KB Output is correct
45 Correct 1348 ms 16104 KB Output is correct
46 Correct 1441 ms 16024 KB Output is correct
47 Correct 1383 ms 15980 KB Output is correct
48 Correct 341 ms 13248 KB Output is correct
49 Correct 343 ms 14968 KB Output is correct
50 Correct 119 ms 5652 KB Output is correct
51 Correct 149 ms 7976 KB Output is correct
52 Correct 57 ms 5376 KB Output is correct
53 Correct 519 ms 14828 KB Output is correct
54 Correct 447 ms 8592 KB Output is correct
55 Correct 1263 ms 13396 KB Output is correct
56 Correct 769 ms 9120 KB Output is correct
57 Correct 1004 ms 14556 KB Output is correct
58 Correct 49 ms 8188 KB Output is correct
59 Correct 119 ms 6748 KB Output is correct
60 Correct 283 ms 14064 KB Output is correct
61 Correct 369 ms 14448 KB Output is correct
62 Correct 166 ms 12912 KB Output is correct
63 Correct 86 ms 10360 KB Output is correct
64 Correct 90 ms 11384 KB Output is correct
65 Correct 130 ms 17224 KB Output is correct
66 Correct 142 ms 6520 KB Output is correct
67 Correct 124 ms 14160 KB Output is correct
68 Correct 294 ms 17400 KB Output is correct
69 Correct 52 ms 4088 KB Output is correct
70 Correct 13 ms 2944 KB Output is correct
71 Correct 111 ms 9656 KB Output is correct
72 Correct 144 ms 15608 KB Output is correct
73 Correct 414 ms 19192 KB Output is correct
74 Correct 601 ms 17144 KB Output is correct
75 Correct 304 ms 19192 KB Output is correct
76 Correct 351 ms 18424 KB Output is correct
77 Correct 538 ms 17508 KB Output is correct