Submission #134047

# Submission time Handle Problem Language Result Execution time Memory
134047 2019-07-22T01:55:10 Z osaaateiasavtnl Cats or Dogs (JOI18_catdog) C++14
100 / 100
1113 ms 26012 KB
#include<bits/stdc++.h>
using namespace std;
#define app push_back
const int N = 1e5 + 7;
const int INF = 1e9 + 7;
struct Dp {
    int a[2][2];
    Dp () {
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 2; ++j) {
                a[i][j] = INF;
            }   
        }   
    }   
    int* operator[](int i) {
        return a[i];
    }   
    Dp operator *(Dp add) {
        Dp ans;        
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 2; ++j) {
                for (int ii = 0; ii < 2; ++ii) {
                    for (int jj = 0; jj < 2; ++jj) {
                        ans[i][jj] = min(ans[i][jj], a[i][j] + add[ii][jj] + (j ^ ii));    
                    }   
                }   
            }   
        }   
        return ans;
    }   
};  
int cnt[N], to[N];
vector <int> g[N];
void dfs1(int u, int p) {
    cnt[u] = 1;
    int mx = -1;
    for (int v : g[u]) {
        if (v != p) {
            dfs1(v, u);
            cnt[u] += cnt[v];
            if (mx == -1 || cnt[mx] < cnt[v]) mx = v;
        }   
    }
    if (mx == -1) {
        to[u] = u;
        return;
    }
    int pos = -1;
    for (int i = 0; i < g[u].size(); ++i) {
        if (g[u][i] == mx) pos = i;
    }   
    swap(g[u][0], g[u][pos]);    
    to[u] = to[mx];
}   
int up[N], pos[N], par[N], ptr = 0;
void dfs2(int u, int p) {
    par[u] = p; pos[u] = ptr++;
    for (int v : g[u]) {
        if (v != p) {
            if (v == g[u][0]) up[v] = up[u];
            else up[v] = v;
            dfs2(v, u);
        }   
    }
}
Dp tree[N << 2];
Dp EM;
Dp get(int v, int tl, int tr, int l, int r) {
    if (tr < l || r < tl) return EM;
    if (l <= tl && tr <= r) return tree[v];
    int tm = (tl + tr) >> 1;
    return get(v * 2 + 1, tl, tm, l, r) * get(v * 2 + 2, tm + 1, tr, l, r);
}   
void relax(int v) {
    tree[v] = tree[v * 2 + 1] * tree[v * 2 + 2];
}
void build(int v, int tl, int tr) {
    if (tl == tr) {
        tree[v] = EM;
        return;
    }   
    int tm = (tl + tr) >> 1;
    build(v * 2 + 1, tl, tm); build(v * 2 + 2, tm + 1, tr);
    relax(v);
}     
void upd(int v, int tl, int tr, int p, int d0, int d1) {
    if (tl == tr) {
        tree[v][0][0] += d0; tree[v][1][1] += d1;
        return;
    }   
    int tm = (tl + tr) >> 1;
    if (p <= tm) upd(v * 2 + 1, tl, tm, p, d0, d1);
    else upd(v * 2 + 2, tm + 1, tr, p, d0, d1);
    relax(v);
}   
void initialize(int n, vector <int> a, vector <int> b) {
    EM[0][0] = EM[1][1] = 0;
    for (int i = 0; i < n - 1; ++i) {
        g[a[i]].app(b[i]); g[b[i]].app(a[i]);
    }       
    up[1] = 1;
    dfs1(1, 0); 
    dfs2(1, 0);
    build(0, 0, N);
}
pair <int, int> get_dp(int v) {
    auto m = get(0, 0, N, pos[v], pos[to[v]]);
    int dp0 = min(m[0][0], m[0][1]), dp1 = min(m[1][0], m[1][1]);
    return {min(dp0, dp1 + 1), min(dp0 + 1, dp1)};
}   
int get() {
    auto m = get(0, 0, N, pos[1], pos[to[1]]);
    int dp0 = min(m[0][0], m[0][1]), dp1 = min(m[1][0], m[1][1]);
    return min(dp0, dp1);
}
void anime(int v, int d0, int d1) {
    while (v) {
        int f0, f1, s0, s1;
        tie(f0, f1) = get_dp(up[v]);
        upd(0, 0, N, pos[v], d0, d1);
        tie(s0, s1) = get_dp(up[v]);
        d0 = s0 - f0; d1 = s1 - f1;
        v = par[up[v]];
    }   
}   
bool type[N];   
int cat(int v) {
    type[v] = 0; anime(v, 0, INF); return get();
}
int dog(int v) {
    type[v] = 1; anime(v, INF, 0); return get();
}
int neighbor(int v) {
    if (type[v]) anime(v, -INF, 0);
    else anime(v, 0, -INF);
    return get();
}   
#ifdef HOME
signed main() {
    freopen("input.txt", "r", stdin);
    int n, q;
    cin >> n >> q;
    vector <int> a, b;
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        a.push_back(u); b.push_back(v);
    }   
    initialize(n, a, b);
    for (int i = 0; i < q; ++i) {
        int t, v;
        cin >> t >> v;
        if (t == 1) cout << cat(v) << '\n';
        else if (t == 2) cout << dog(v) << '\n';
        else cout << neighbor(v) << '\n';
    }   
}   
#endif
/*
5 3
1 2
1 4
2 3
2 4
1 3
2 5
2 4
*/

Compilation message

catdog.cpp: In function 'void dfs1(int, int)':
catdog.cpp:49:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[u].size(); ++i) {
                     ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 8952 KB Output is correct
2 Correct 12 ms 8952 KB Output is correct
3 Correct 12 ms 8952 KB Output is correct
4 Correct 12 ms 8952 KB Output is correct
5 Correct 13 ms 8924 KB Output is correct
6 Correct 12 ms 8956 KB Output is correct
7 Correct 12 ms 8952 KB Output is correct
8 Correct 12 ms 8952 KB Output is correct
9 Correct 12 ms 8952 KB Output is correct
10 Correct 12 ms 8952 KB Output is correct
11 Correct 12 ms 8952 KB Output is correct
12 Correct 12 ms 8952 KB Output is correct
13 Correct 12 ms 8992 KB Output is correct
14 Correct 13 ms 8952 KB Output is correct
15 Correct 12 ms 8952 KB Output is correct
16 Correct 12 ms 8952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 8952 KB Output is correct
2 Correct 12 ms 8952 KB Output is correct
3 Correct 12 ms 8952 KB Output is correct
4 Correct 12 ms 8952 KB Output is correct
5 Correct 13 ms 8924 KB Output is correct
6 Correct 12 ms 8956 KB Output is correct
7 Correct 12 ms 8952 KB Output is correct
8 Correct 12 ms 8952 KB Output is correct
9 Correct 12 ms 8952 KB Output is correct
10 Correct 12 ms 8952 KB Output is correct
11 Correct 12 ms 8952 KB Output is correct
12 Correct 12 ms 8952 KB Output is correct
13 Correct 12 ms 8992 KB Output is correct
14 Correct 13 ms 8952 KB Output is correct
15 Correct 12 ms 8952 KB Output is correct
16 Correct 12 ms 8952 KB Output is correct
17 Correct 16 ms 8952 KB Output is correct
18 Correct 17 ms 9080 KB Output is correct
19 Correct 15 ms 9080 KB Output is correct
20 Correct 12 ms 8952 KB Output is correct
21 Correct 14 ms 8952 KB Output is correct
22 Correct 14 ms 8952 KB Output is correct
23 Correct 17 ms 9080 KB Output is correct
24 Correct 17 ms 9080 KB Output is correct
25 Correct 16 ms 8952 KB Output is correct
26 Correct 14 ms 8952 KB Output is correct
27 Correct 13 ms 8952 KB Output is correct
28 Correct 13 ms 9080 KB Output is correct
29 Correct 15 ms 9080 KB Output is correct
30 Correct 14 ms 8952 KB Output is correct
31 Correct 13 ms 8952 KB Output is correct
32 Correct 14 ms 8952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 8952 KB Output is correct
2 Correct 12 ms 8952 KB Output is correct
3 Correct 12 ms 8952 KB Output is correct
4 Correct 12 ms 8952 KB Output is correct
5 Correct 13 ms 8924 KB Output is correct
6 Correct 12 ms 8956 KB Output is correct
7 Correct 12 ms 8952 KB Output is correct
8 Correct 12 ms 8952 KB Output is correct
9 Correct 12 ms 8952 KB Output is correct
10 Correct 12 ms 8952 KB Output is correct
11 Correct 12 ms 8952 KB Output is correct
12 Correct 12 ms 8952 KB Output is correct
13 Correct 12 ms 8992 KB Output is correct
14 Correct 13 ms 8952 KB Output is correct
15 Correct 12 ms 8952 KB Output is correct
16 Correct 12 ms 8952 KB Output is correct
17 Correct 16 ms 8952 KB Output is correct
18 Correct 17 ms 9080 KB Output is correct
19 Correct 15 ms 9080 KB Output is correct
20 Correct 12 ms 8952 KB Output is correct
21 Correct 14 ms 8952 KB Output is correct
22 Correct 14 ms 8952 KB Output is correct
23 Correct 17 ms 9080 KB Output is correct
24 Correct 17 ms 9080 KB Output is correct
25 Correct 16 ms 8952 KB Output is correct
26 Correct 14 ms 8952 KB Output is correct
27 Correct 13 ms 8952 KB Output is correct
28 Correct 13 ms 9080 KB Output is correct
29 Correct 15 ms 9080 KB Output is correct
30 Correct 14 ms 8952 KB Output is correct
31 Correct 13 ms 8952 KB Output is correct
32 Correct 14 ms 8952 KB Output is correct
33 Correct 694 ms 14308 KB Output is correct
34 Correct 193 ms 14520 KB Output is correct
35 Correct 639 ms 13552 KB Output is correct
36 Correct 1002 ms 17852 KB Output is correct
37 Correct 33 ms 11384 KB Output is correct
38 Correct 1085 ms 18772 KB Output is correct
39 Correct 1045 ms 18668 KB Output is correct
40 Correct 1069 ms 18660 KB Output is correct
41 Correct 1084 ms 18792 KB Output is correct
42 Correct 1083 ms 18736 KB Output is correct
43 Correct 1053 ms 18640 KB Output is correct
44 Correct 991 ms 18760 KB Output is correct
45 Correct 1031 ms 18664 KB Output is correct
46 Correct 1012 ms 18664 KB Output is correct
47 Correct 1113 ms 18660 KB Output is correct
48 Correct 326 ms 16064 KB Output is correct
49 Correct 344 ms 17540 KB Output is correct
50 Correct 151 ms 11104 KB Output is correct
51 Correct 155 ms 12404 KB Output is correct
52 Correct 75 ms 10744 KB Output is correct
53 Correct 397 ms 17400 KB Output is correct
54 Correct 350 ms 12976 KB Output is correct
55 Correct 907 ms 16188 KB Output is correct
56 Correct 587 ms 13728 KB Output is correct
57 Correct 717 ms 17144 KB Output is correct
58 Correct 57 ms 12404 KB Output is correct
59 Correct 145 ms 12252 KB Output is correct
60 Correct 298 ms 16760 KB Output is correct
61 Correct 365 ms 17264 KB Output is correct
62 Correct 199 ms 15344 KB Output is correct
63 Correct 92 ms 16120 KB Output is correct
64 Correct 90 ms 17656 KB Output is correct
65 Correct 116 ms 22364 KB Output is correct
66 Correct 170 ms 12664 KB Output is correct
67 Correct 131 ms 18552 KB Output is correct
68 Correct 280 ms 22856 KB Output is correct
69 Correct 82 ms 10488 KB Output is correct
70 Correct 30 ms 9208 KB Output is correct
71 Correct 128 ms 15608 KB Output is correct
72 Correct 176 ms 20960 KB Output is correct
73 Correct 474 ms 26012 KB Output is correct
74 Correct 504 ms 22392 KB Output is correct
75 Correct 295 ms 25976 KB Output is correct
76 Correct 285 ms 24568 KB Output is correct
77 Correct 496 ms 22776 KB Output is correct