Submission #482224

# Submission time Handle Problem Language Result Execution time Memory
482224 2021-10-23T19:09:16 Z SirCovidThe19th Cats or Dogs (JOI18_catdog) C++17
100 / 100
433 ms 31684 KB
#include <bits/stdc++.h>
#include "catdog.h"
using namespace std;

#define FOR(i, x, y) for (int i = x; i < y; i++)
#define mat array<array<int, 2>, 2>

const int mx = 1e5 + 5;

int state[mx], par[mx], sz[mx], dep[mx], root[mx], id[mx], psz[mx]; vector<int> adj[mx];

struct segTree{
    int sz = 1; vector<mat> seg; 
    void init(int sz_){ 
        while (sz < sz_) sz *= 2; 
        seg.resize(sz * 2, {}); 
        FOR(i, 1, sz * 2) seg[i][0][1] = seg[i][1][0] = 1e9;
    }
    void comb(mat &cur, mat A, mat B){ 
        FOR(i, 0, 2) FOR(j, 0, 2) cur[i][j] = 1e9;
        FOR(i, 0, 2) FOR(j, 0, 2) FOR(k, 0, 2) FOR(l, 0, 2)
            cur[i][j] = min(cur[i][j], B[i][k] + A[l][j] + (k != l));
    }
    void apply(int i, int dt0, int dt1){
        i += sz;
        seg[i][0][0] += dt0;
        seg[i][1][1] += dt1;
        while (i /= 2) comb(seg[i], seg[i * 2], seg[i * 2 + 1]); 
    }
    void getBestTop(int &A, int &B){ 
        int top0 = min(seg[1][0][0], seg[1][0][1]);
        int top1 = min(seg[1][1][0], seg[1][1][1]); 
        A = min(top0, top1 + 1);
        B = min(top0 + 1, top1);
    }
}trees[mx];

void dfsSZ(int u){
    sz[u] = 1;
    for (int &v : adj[u]) if (v != par[u]){
        par[v] = u; dep[v] = dep[u] + 1;
        dfsSZ(v); 
        sz[u] += sz[v];
        if (sz[v] > sz[adj[u][0]]) swap(v, adj[u][0]);
    }
}
void dfsHLD(int u){
    id[u] = psz[root[u]]++;
    for (int v : adj[u]) if (v != par[u]){
        root[v] = (v == adj[u][0] ? root[u] : v);
        dfsHLD(v);
    }
}
int updSolve(int u, int col, int dt0, int dt1){
    state[u] = col;
    while (u){
        int b0, b1, n0, n1, R = root[u];
        trees[R].getBestTop(b0, b1);
        trees[R].apply(id[u], dt0, dt1);
        trees[R].getBestTop(n0, n1);
        dt0 = n0 - b0; dt1 = n1 - b1;
        u = par[R];
    }
    int ret0, ret1; trees[1].getBestTop(ret0, ret1);
    return min(ret0, ret1);
}
void initialize(int n, vector<int> A, vector<int> B){ 
    FOR(i, 0, n - 1){
        adj[A[i]].push_back(B[i]);
        adj[B[i]].push_back(A[i]);
    }
    root[1] = id[1] = 1; dfsSZ(1); dfsHLD(1);
    FOR(i, 1, n + 1){
        trees[i].init(psz[i]);
        id[i] = psz[root[i]] - id[i] - 1; 
    }
}
int cat(int i){ return updSolve(i, 0, 0, mx); }
int dog(int i){ return updSolve(i, 1, mx, 0); }
int neighbor(int i){ return !state[i] ? updSolve(i, 0, 0, -mx) : updSolve(i, 1, -mx, 0); }
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5708 KB Output is correct
2 Correct 3 ms 5708 KB Output is correct
3 Correct 3 ms 5808 KB Output is correct
4 Correct 3 ms 5708 KB Output is correct
5 Correct 4 ms 5708 KB Output is correct
6 Correct 3 ms 5708 KB Output is correct
7 Correct 3 ms 5708 KB Output is correct
8 Correct 3 ms 5708 KB Output is correct
9 Correct 3 ms 5776 KB Output is correct
10 Correct 3 ms 5708 KB Output is correct
11 Correct 3 ms 5708 KB Output is correct
12 Correct 3 ms 5708 KB Output is correct
13 Correct 4 ms 5832 KB Output is correct
14 Correct 3 ms 5708 KB Output is correct
15 Correct 3 ms 5708 KB Output is correct
16 Correct 3 ms 5768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5708 KB Output is correct
2 Correct 3 ms 5708 KB Output is correct
3 Correct 3 ms 5808 KB Output is correct
4 Correct 3 ms 5708 KB Output is correct
5 Correct 4 ms 5708 KB Output is correct
6 Correct 3 ms 5708 KB Output is correct
7 Correct 3 ms 5708 KB Output is correct
8 Correct 3 ms 5708 KB Output is correct
9 Correct 3 ms 5776 KB Output is correct
10 Correct 3 ms 5708 KB Output is correct
11 Correct 3 ms 5708 KB Output is correct
12 Correct 3 ms 5708 KB Output is correct
13 Correct 4 ms 5832 KB Output is correct
14 Correct 3 ms 5708 KB Output is correct
15 Correct 3 ms 5708 KB Output is correct
16 Correct 3 ms 5768 KB Output is correct
17 Correct 4 ms 5836 KB Output is correct
18 Correct 6 ms 5836 KB Output is correct
19 Correct 4 ms 5920 KB Output is correct
20 Correct 4 ms 5780 KB Output is correct
21 Correct 4 ms 5836 KB Output is correct
22 Correct 3 ms 5836 KB Output is correct
23 Correct 4 ms 5836 KB Output is correct
24 Correct 5 ms 5916 KB Output is correct
25 Correct 4 ms 5788 KB Output is correct
26 Correct 3 ms 5836 KB Output is correct
27 Correct 3 ms 5836 KB Output is correct
28 Correct 3 ms 5964 KB Output is correct
29 Correct 4 ms 5964 KB Output is correct
30 Correct 3 ms 5808 KB Output is correct
31 Correct 4 ms 5836 KB Output is correct
32 Correct 3 ms 5784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5708 KB Output is correct
2 Correct 3 ms 5708 KB Output is correct
3 Correct 3 ms 5808 KB Output is correct
4 Correct 3 ms 5708 KB Output is correct
5 Correct 4 ms 5708 KB Output is correct
6 Correct 3 ms 5708 KB Output is correct
7 Correct 3 ms 5708 KB Output is correct
8 Correct 3 ms 5708 KB Output is correct
9 Correct 3 ms 5776 KB Output is correct
10 Correct 3 ms 5708 KB Output is correct
11 Correct 3 ms 5708 KB Output is correct
12 Correct 3 ms 5708 KB Output is correct
13 Correct 4 ms 5832 KB Output is correct
14 Correct 3 ms 5708 KB Output is correct
15 Correct 3 ms 5708 KB Output is correct
16 Correct 3 ms 5768 KB Output is correct
17 Correct 4 ms 5836 KB Output is correct
18 Correct 6 ms 5836 KB Output is correct
19 Correct 4 ms 5920 KB Output is correct
20 Correct 4 ms 5780 KB Output is correct
21 Correct 4 ms 5836 KB Output is correct
22 Correct 3 ms 5836 KB Output is correct
23 Correct 4 ms 5836 KB Output is correct
24 Correct 5 ms 5916 KB Output is correct
25 Correct 4 ms 5788 KB Output is correct
26 Correct 3 ms 5836 KB Output is correct
27 Correct 3 ms 5836 KB Output is correct
28 Correct 3 ms 5964 KB Output is correct
29 Correct 4 ms 5964 KB Output is correct
30 Correct 3 ms 5808 KB Output is correct
31 Correct 4 ms 5836 KB Output is correct
32 Correct 3 ms 5784 KB Output is correct
33 Correct 100 ms 14876 KB Output is correct
34 Correct 49 ms 15624 KB Output is correct
35 Correct 81 ms 12564 KB Output is correct
36 Correct 149 ms 21116 KB Output is correct
37 Correct 17 ms 10316 KB Output is correct
38 Correct 171 ms 22668 KB Output is correct
39 Correct 162 ms 22720 KB Output is correct
40 Correct 165 ms 22724 KB Output is correct
41 Correct 164 ms 22708 KB Output is correct
42 Correct 174 ms 22920 KB Output is correct
43 Correct 157 ms 22704 KB Output is correct
44 Correct 160 ms 22780 KB Output is correct
45 Correct 169 ms 22836 KB Output is correct
46 Correct 161 ms 22804 KB Output is correct
47 Correct 158 ms 22736 KB Output is correct
48 Correct 55 ms 16652 KB Output is correct
49 Correct 64 ms 19196 KB Output is correct
50 Correct 22 ms 8772 KB Output is correct
51 Correct 27 ms 11116 KB Output is correct
52 Correct 13 ms 8488 KB Output is correct
53 Correct 125 ms 21064 KB Output is correct
54 Correct 58 ms 12668 KB Output is correct
55 Correct 136 ms 17852 KB Output is correct
56 Correct 77 ms 13696 KB Output is correct
57 Correct 110 ms 20368 KB Output is correct
58 Correct 21 ms 11336 KB Output is correct
59 Correct 40 ms 10676 KB Output is correct
60 Correct 61 ms 17732 KB Output is correct
61 Correct 63 ms 18348 KB Output is correct
62 Correct 44 ms 15644 KB Output is correct
63 Correct 37 ms 17320 KB Output is correct
64 Correct 124 ms 19780 KB Output is correct
65 Correct 66 ms 28996 KB Output is correct
66 Correct 44 ms 11432 KB Output is correct
67 Correct 249 ms 21620 KB Output is correct
68 Correct 433 ms 28316 KB Output is correct
69 Correct 25 ms 7812 KB Output is correct
70 Correct 9 ms 6124 KB Output is correct
71 Correct 45 ms 16380 KB Output is correct
72 Correct 91 ms 25420 KB Output is correct
73 Correct 160 ms 31640 KB Output is correct
74 Correct 160 ms 28056 KB Output is correct
75 Correct 122 ms 31684 KB Output is correct
76 Correct 116 ms 31256 KB Output is correct
77 Correct 171 ms 28500 KB Output is correct