Submission #253921

# Submission time Handle Problem Language Result Execution time Memory
253921 2020-07-29T06:07:01 Z IgorI Cats or Dogs (JOI18_catdog) C++17
100 / 100
327 ms 52776 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e8;
const int N = 100002;

struct Table{
    int a00, a01, a10, a11;
    Table()
    {
        a00 = a11 = 0;
        a01 = a10 = 1;
    }
};

Table Merge(Table &A, Table &B)
{
    Table S;
    S.a00 = min(A.a00 + B.a00, A.a01 + B.a10);
    S.a01 = min(A.a00 + B.a01, A.a01 + B.a11);
    S.a11 = min(A.a10 + B.a01, A.a11 + B.a11);
    S.a10 = min(A.a10 + B.a00, A.a11 + B.a10);
    return S;
}

struct Sollution{
    int s;
    vector<Table> tree;
    Sollution(int _n)
    {
        for (s = 1; s < _n; s *= 2) ;
        tree.resize(2 * s);
    }
    void Change(int i, int &cost0, int &cost1)
    {
        i += s;
        tree[i].a00 += cost0;
        tree[i].a01 += cost0;
        tree[i].a10 += cost1;
        tree[i].a11 += cost1;
        for (i /= 2; i; i /= 2)
            tree[i] = Merge(tree[2 * i], tree[2 * i + 1]);
    }
    void Cost(int &d0, int &d1)
    {
        int minc0 = min(tree[1].a00, tree[1].a01), minc1 = min(tree[1].a10, tree[1].a11);
        d0 = min(minc0, minc1 + 1);
        d1 = min(minc0 + 1, minc1);
    }
};

ll n;
vector<int> graph[N];
ll x[N];
ll papa[N];
ll sz[N];

int main_child[N];
vector<int> other_child[N];
vector<vector<int> > route;
vector<Sollution> hld;
int route_pos[N];
int route_id[N];

void dfs1(int v, int p)
{
    papa[v] = p;
    sz[v] = 1;
    for (auto u : graph[v]) if (u != p)
    {
        dfs1(u, v);
        sz[v] += sz[u];
    }
}

void dfs2(int v, int p)
{
    int mxsz = 0, id = -1;
    for (auto u : graph[v]) if (u != p)
    {
        if (sz[u] > mxsz)
        {
            mxsz = sz[u];
            id = u;
        }
    }
    main_child[v] = id;
    for (auto u : graph[v]) if (u != p)
    {
        dfs2(u, v);
        if (u != id) other_child[v].push_back(u);
    }
}

int answer()
{
    Table T = hld[0].tree[1];
    return min(min(T.a00, T.a01), min(T.a10, T.a11));
}

void initialize(int n0, vector<int> a, vector<int> b)
{
    n = n0;
    for (int i = 0; i < n - 1; i++)
    {
        a[i]--, b[i]--;
        graph[a[i]].push_back(b[i]);
        graph[b[i]].push_back(a[i]);
    }
    for (int i = 0; i < n; i++)
    {
        x[i] = -1;
    }
    dfs1(0, 0);
    dfs2(0, 0);
    for (int i = 0; i < n; i++)
    {
        route_id[i] = -1;
        route_pos[i] = -1;
    }
    for (int i = 0; i < n; i++)
    {
        if (route_id[i] == -1)
        {
            vector<int> r;
            int x = i;
            while (x != -1)
            {
                r.push_back(x);
                x = main_child[x];
            }
            for (int j = 0; j < r.size(); j++)
            {
                route_id[r[j]] = route.size();
                route_pos[r[j]] = j;
            }
            route.push_back(r);
            hld.push_back(Sollution(r.size()));
        }
    }
}

void Change(int v, int cost0, int cost1)
{
    while (1)
    {
        int x = route_id[v];
        int y = route_pos[v];
        int oldd0, oldd1, d0, d1;
        hld[x].Cost(oldd0, oldd1);
        hld[x].Change(y, cost0, cost1);
        hld[x].Cost(d0, d1);
        v = route[x][0];
        if (v == 0) return;
        v = papa[v], cost0 = d0 - oldd0, cost1 = d1 - oldd1;
    }
}

int cat(int v)
{
    v--;
    x[v] = 0;
    Change(v, 0, INF);
    return answer();
}

int dog(int v)
{
    v--;
    x[v] = 1;
    Change(v, INF, 0);
    return answer();
}

int neighbor(int v)
{
    v--;
    if (x[v] == 0) Change(v, 0, -INF);
    if (x[v] == 1) Change(v, -INF, 0);
    x[v] = -1;
    return answer();
}

Compilation message

catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:135:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int j = 0; j < r.size(); j++)
                             ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 3 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 3 ms 4992 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 3 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 3 ms 4992 KB Output is correct
9 Correct 4 ms 4992 KB Output is correct
10 Correct 3 ms 4992 KB Output is correct
11 Correct 4 ms 4992 KB Output is correct
12 Correct 3 ms 4992 KB Output is correct
13 Correct 3 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 3 ms 4992 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 3 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 3 ms 4992 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 3 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 3 ms 4992 KB Output is correct
9 Correct 4 ms 4992 KB Output is correct
10 Correct 3 ms 4992 KB Output is correct
11 Correct 4 ms 4992 KB Output is correct
12 Correct 3 ms 4992 KB Output is correct
13 Correct 3 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 3 ms 4992 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
17 Correct 4 ms 5248 KB Output is correct
18 Correct 4 ms 5224 KB Output is correct
19 Correct 4 ms 5248 KB Output is correct
20 Correct 3 ms 5120 KB Output is correct
21 Correct 5 ms 5120 KB Output is correct
22 Correct 4 ms 5120 KB Output is correct
23 Correct 5 ms 5248 KB Output is correct
24 Correct 4 ms 5248 KB Output is correct
25 Correct 4 ms 5120 KB Output is correct
26 Correct 4 ms 5120 KB Output is correct
27 Correct 3 ms 5120 KB Output is correct
28 Correct 4 ms 5248 KB Output is correct
29 Correct 4 ms 5248 KB Output is correct
30 Correct 4 ms 5120 KB Output is correct
31 Correct 4 ms 5248 KB Output is correct
32 Correct 4 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 3 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 3 ms 4992 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 3 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 3 ms 4992 KB Output is correct
9 Correct 4 ms 4992 KB Output is correct
10 Correct 3 ms 4992 KB Output is correct
11 Correct 4 ms 4992 KB Output is correct
12 Correct 3 ms 4992 KB Output is correct
13 Correct 3 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 3 ms 4992 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
17 Correct 4 ms 5248 KB Output is correct
18 Correct 4 ms 5224 KB Output is correct
19 Correct 4 ms 5248 KB Output is correct
20 Correct 3 ms 5120 KB Output is correct
21 Correct 5 ms 5120 KB Output is correct
22 Correct 4 ms 5120 KB Output is correct
23 Correct 5 ms 5248 KB Output is correct
24 Correct 4 ms 5248 KB Output is correct
25 Correct 4 ms 5120 KB Output is correct
26 Correct 4 ms 5120 KB Output is correct
27 Correct 3 ms 5120 KB Output is correct
28 Correct 4 ms 5248 KB Output is correct
29 Correct 4 ms 5248 KB Output is correct
30 Correct 4 ms 5120 KB Output is correct
31 Correct 4 ms 5248 KB Output is correct
32 Correct 4 ms 5120 KB Output is correct
33 Correct 118 ms 16916 KB Output is correct
34 Correct 93 ms 19340 KB Output is correct
35 Correct 112 ms 13340 KB Output is correct
36 Correct 303 ms 25448 KB Output is correct
37 Correct 31 ms 12012 KB Output is correct
38 Correct 240 ms 27792 KB Output is correct
39 Correct 229 ms 27792 KB Output is correct
40 Correct 233 ms 27788 KB Output is correct
41 Correct 246 ms 28028 KB Output is correct
42 Correct 233 ms 27920 KB Output is correct
43 Correct 235 ms 27788 KB Output is correct
44 Correct 228 ms 27788 KB Output is correct
45 Correct 231 ms 27792 KB Output is correct
46 Correct 327 ms 27920 KB Output is correct
47 Correct 236 ms 27792 KB Output is correct
48 Correct 101 ms 22752 KB Output is correct
49 Correct 132 ms 25992 KB Output is correct
50 Correct 35 ms 9516 KB Output is correct
51 Correct 55 ms 13816 KB Output is correct
52 Correct 24 ms 9520 KB Output is correct
53 Correct 173 ms 27040 KB Output is correct
54 Correct 84 ms 14428 KB Output is correct
55 Correct 199 ms 20632 KB Output is correct
56 Correct 114 ms 15436 KB Output is correct
57 Correct 165 ms 25244 KB Output is correct
58 Correct 37 ms 14492 KB Output is correct
59 Correct 45 ms 12452 KB Output is correct
60 Correct 99 ms 23772 KB Output is correct
61 Correct 111 ms 24408 KB Output is correct
62 Correct 83 ms 22492 KB Output is correct
63 Correct 64 ms 22680 KB Output is correct
64 Correct 60 ms 25908 KB Output is correct
65 Correct 89 ms 39008 KB Output is correct
66 Correct 47 ms 10872 KB Output is correct
67 Correct 80 ms 29488 KB Output is correct
68 Correct 131 ms 42108 KB Output is correct
69 Correct 24 ms 6784 KB Output is correct
70 Correct 8 ms 5248 KB Output is correct
71 Correct 51 ms 14688 KB Output is correct
72 Correct 87 ms 23776 KB Output is correct
73 Correct 133 ms 28276 KB Output is correct
74 Correct 182 ms 52776 KB Output is correct
75 Correct 147 ms 29172 KB Output is correct
76 Correct 151 ms 33972 KB Output is correct
77 Correct 179 ms 46484 KB Output is correct