Submission #400934

# Submission time Handle Problem Language Result Execution time Memory
400934 2021-05-08T20:59:00 Z 12tqian Stray Cat (JOI20_stray) C++17
100 / 100
81 ms 17532 KB
#include "Anthony.h"
#include <bits/stdc++.h>

using namespace std;

#define f1r(i, a, b) for (int i = a; i < b; ++i)
#define f0r(i, a) f1r(i, 0, a)
#define each(t, a) for (auto& t : a)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

using vi = vector<int>;
using pi = pair<int, int>;
using vpi = vector<pi>;
using ll = long long;

// 0 -> 1 -> 2

vi Mark(int n, int m, int a, int b, vi U, vi V) {
    vector<vi> g(n);
    auto cp = [&](int u, int v) -> pi {
        if (u > v) swap(u, v);
        return mp(u, v);
    };
    map<pi, int> conv;
    vpi ed;
    vi res(m);
    f0r(i, m) {
        int u = U[i];
        int v = V[i];
        assert(u < n);
        assert(v < n);
        conv[cp(u, v)] = i;
        g[u].pb(v);
        g[v].pb(u);
        ed.pb(cp(u, v));
    }
    if (a >= 3) { // bfs work
        vi dist(n, -1);
        list<int> que;
        dist[0] = 0;
        que.pb(0);
        while (!que.empty()) {
            int u = que.front();
            que.pop_front();
            each(v, g[u]) {
                if (dist[v] != -1) continue;
                dist[v] = dist[u] + 1;
                que.push_back(v);
            }
        }
        each(e, ed) {
            int u = e.f;
            int v = e.s;
            int id = conv[cp(u, v)];
            if (dist[u] == dist[v]) {
                res[id] = dist[u] % 3;
            } else {
                if (dist[u] > dist[v]) {
                    swap(u, v);
                }
                res[id] = dist[u] % 3;
            }
        }
    } else {
        assert(n - 1 == m);
        vi par(n);
        int cnt = 0;
        function<void(int, int)> dfs_precomp = [&](int u, int p) {
            assert(u < n);
            par[u] = p;
            each(v, g[u]) {
                if (v == p) continue;
                dfs_precomp(v, u);
            }
        };
        dfs_precomp(0, -1);
        vi lab(n);
        function<void(int, int, int)> dfs_label = [&](int u, int p, int d) {
            if (u == 0) {
                each(v, g[u]) {
                    if (v == p) continue;
                    lab[v] = 0;
                    dfs_label(v, u, 0);
                }
                return;
            }
            if (sz(g[u]) == 1) return;
            if (sz(g[u]) > 2) {
                each(v, g[u])  {
                    if (v == p) continue;
                    lab[v] = lab[u] ^ 1;
                    dfs_label(v, u, 0);
                }
            } else {
                each(v, g[u]) {
                    if (v == p) continue;
                    if (d == 0 || d == 2 || d == 5) {
                        lab[v] = lab[u] ^ 1;
                    } else {
                        lab[v] = lab[u];
                    }
                    dfs_label(v, u, (d + 1) % 6);
                }
            }
        };
        dfs_label(0, -1, -1);
        f1r(i, 1, n) {
            int id = conv[cp(i, par[i])];
            res[id] = lab[i];
        }
    }
    return res;
}

#include "Catherine.h"
#include <bits/stdc++.h>

using namespace std;

#define f1r(i, a, b) for (int i = a; i < b; ++i)
#define f0r(i, a) f1r(i, 0, a)
#define each(t, a) for (auto& t : a)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

using vi = vector<int>;
using pi = pair<int, int>;
using vpi = vector<pi>;
using ll = long long;

namespace {

int a, b;
vi moves; 
vpi seen;
bool oriented = false;

}  // namespace

void Init(int A, int B) {
    ::a = A;
    ::b = B;
}

int MoveT(vi& v) { 
    // cout << "MOVE" << endl;
    int x = v[0]; 
    int y = v[1];
    
    pi use = {x, y};
   
    if (sz(moves)) {
        if (moves.back() == 0) use.f++;
        else use.s++;
    }

    seen.pb(use);

    #define R(k) moves.pb(k); return k;
    #define F() if (x) { R(0); } else { R(1); }
    // R is for moving to a color
    // F is if you're on a line, move forward

    if (oriented) { // if you're already oriented
        assert(x || y);
        if (x == 0) {
            R(1);
        } else if (y == 0) {
            R(0);
        }

        if (sz(moves)) {
            if (moves.back() == 0) x++;
            else y++;
        }
        
        if (x > y) {
            assert(y == 1);
            R(1);
        } else if (x < y) {
            assert(x == 1);
            R(0);
        }

        assert(false);
        R(-1); // shouldn't reach here
    }

    { // checking if you're already at a good place
        if (x == 0 && y == 0) { // dead end leaf node
            oriented = true;
            R(-1);
        }

        if (sz(moves)) {
            if (moves.back() == 0) x++;
            else y++;
        }
        
        if (x == 0 && y == 1) { // leaf nodes
            oriented = true;
            R(1);
        } else if (y == 0 && x == 1) {
            oriented = true;
            R(0);
        } 
        
        if (x > y && y) { // you already reached a good place
            oriented = true;
            assert(y == 1);
            if (sz(moves)) {
                if (moves.back() == 0) x--;
                else y--;
            }
            if (y == 0) {
                R(-1);
            }
            else {
                R(1);
            } 
        } else if (x < y && x) {
            oriented = true;
            assert(x == 1);
            if (sz(moves)) {
                if (moves.back() == 0) x--;
                else y--;
            }
            if (x == 0) {
                R(-1); 
            } else {
                R(0);
            }
        }

        if (sz(moves)) {
            if (moves.back() == 0) x--;
            else y--;
        }
    }
    // each(x, moves) cout << x << " ";
    // cout << endl;
    // each(x, seen) cout << "(" << x.f << ", " << x.s << ") ";
    // cout << endl;

    int did = sz(moves);

    if (did == 0) { // first move
        F();
    } else if (did == 1) { 
        if (seen[0].f == seen[0].s) { // 0 1, went 0 before
            if (x) { // go 0 if possible
                R(0);
            } else {
                R(-1);
            }
        } else { // same things
            if (moves.back() == 0 && x) {
                R(-1);
            } else if (moves.back() == 1 && y) {
                R(-1);
            } else {
                F();
            }
        }
    } else if (did == 2) {
        if (moves.back() == -1) {
            F();
        } else {
            R(-1);
        }
    } else if (did == 3) {
        F();
    } else if (did == 4) {
        if (moves[2] == -1) {
            F();
        } else {
            R(-1);
        }
    } else if (did == 5) {
        if (moves.back() == -1) {
            F();
        } else {
            R(-1);
        }
    } else if (did == 6) {
        oriented = true;
        vi came; // direction just came from
        vi other; // other direction
        { // came
            if (moves[4] == -1 && moves[5] != -1) {
                came.pb(moves[2]);
                came.pb(moves[3]);
                if (seen[4].f >= 2) {
                    came.pb(0);
                } else if (seen[4].s >= 2) {
                    came.pb(1);
                } else {
                    came.pb(came.back() ^ 1);
                }
            } else {
                came.pb(moves[4]); 
                if (seen[5].f >= 2) {
                    came.pb(0);
                } else if (seen[5].s >= 2) {
                    came.pb(1);
                } else {
                    came.pb(came.back() ^ 1);
                }
            }
        }
        { // other
            if (moves[0] != -1 && moves[1] != -1) {
                other.pb(moves[0]);
                other.pb(moves[1]);
                if (seen[2].f >= 2) {
                    other.pb(0);
                } else if (seen[2].s >= 2) {
                    other.pb(1);
                } else {
                    other.pb(other.back() ^ 1);
                }
            } else {
                other.pb(moves[0]);
                if (seen[1].f >= 2) {
                    other.pb(0);
                } else if (seen[1].s >= 2) {
                    other.pb(1);
                } else {
                    other.pb(other.back() ^ 1);
                }
            }
        }
        vi comb = came;
        reverse(all(comb));
        each(x, other) comb.pb(x);
        // cout << "COMB" << endl;
        // each(x, comb) cout << x << " ";
        // cout << endl;
        vi use;
        int i1 = 0;
        int i2 = 0;
        int sz = sz(comb);
        while (i1 != sz) {
            while (i2 < sz - 1 && comb[i2 + 1] == comb[i2]) ++i2;
            use.pb(i2 - i1 + 1);
            i1 = ++i2;
        }
        int num = 0;
        each(x, use) num += x;
        
        assert(num == 5);

        if (sz(use) == 2) {
            int u = use[0];
            int v = use[1];
            if (u == 2) {
                R(-1);
            } else {
                F();
            } 
        } else if (sz(use) == 3) {
            int u = use[0];
            int v = use[1];
            int w = use[2];
            if (u == 1) {
                if (v == 2) {
                    R(-1);
                } else if (v == 1) {
                    F();
                } else {
                    assert(false);
                }
            } else if (u == 2) {
                if (v == 2) {
                    F();
                } else {
                    assert(false);
                }
            } else if (u == 3) {
                R(-1);
            }
        } else {
            if (use[1] == 2) {
                F();
            } else {
                R(-1);
            }
        }
    } else {
        assert(false);
    }
    // cout << did << endl;
    // cout << "YA" << endl;
    assert(false);
    R(-1);
}

int MoveG(vi& y) {
    int a0 = y[0];
    int a1 = y[1];
    int a2 = y[2];
    if (a0 && a1) {
        return 0;
    } else if (a1 && a2) {
        return 1;
    } else if (a2 && a0) {
        return 2;
    } else if (a0) {
        return 0;
    } else if (a1) {
        return 1;
    } else if (a2) {
        return 2;
    }
    return -1;
}

int Move(vi y) {
    if (a >= 3) {
        return MoveG(y);
    } else {
        return MoveT(y);    
    }
}

Compilation message

Anthony.cpp: In function 'vi Mark(int, int, int, int, vi, vi)':
Anthony.cpp:74:13: warning: unused variable 'cnt' [-Wunused-variable]
   74 |         int cnt = 0;
      |             ^~~

Catherine.cpp: In function 'int MoveT(vi&)':
Catherine.cpp:247:17: warning: unused variable 'v' [-Wunused-variable]
  247 |             int v = use[1];
      |                 ^
Catherine.cpp:256:17: warning: unused variable 'w' [-Wunused-variable]
  256 |             int w = use[2];
      |                 ^
# Verdict Execution time Memory Grader output
1 Correct 63 ms 16464 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 49 ms 16376 KB Output is correct
4 Correct 67 ms 17532 KB Output is correct
5 Correct 77 ms 17516 KB Output is correct
6 Correct 71 ms 16204 KB Output is correct
7 Correct 57 ms 16236 KB Output is correct
8 Correct 63 ms 16904 KB Output is correct
9 Correct 75 ms 16832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 16464 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 49 ms 16376 KB Output is correct
4 Correct 67 ms 17532 KB Output is correct
5 Correct 77 ms 17516 KB Output is correct
6 Correct 71 ms 16204 KB Output is correct
7 Correct 57 ms 16236 KB Output is correct
8 Correct 63 ms 16904 KB Output is correct
9 Correct 75 ms 16832 KB Output is correct
10 Correct 55 ms 14304 KB Output is correct
11 Correct 65 ms 14296 KB Output is correct
12 Correct 60 ms 14072 KB Output is correct
13 Correct 60 ms 14164 KB Output is correct
14 Correct 60 ms 14292 KB Output is correct
15 Correct 65 ms 14784 KB Output is correct
16 Correct 66 ms 17068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 14012 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 54 ms 13780 KB Output is correct
4 Correct 80 ms 15352 KB Output is correct
5 Correct 72 ms 15284 KB Output is correct
6 Correct 56 ms 13932 KB Output is correct
7 Correct 60 ms 13956 KB Output is correct
8 Correct 66 ms 14556 KB Output is correct
9 Correct 66 ms 14620 KB Output is correct
10 Correct 63 ms 14412 KB Output is correct
11 Correct 56 ms 14404 KB Output is correct
12 Correct 57 ms 14356 KB Output is correct
13 Correct 65 ms 14320 KB Output is correct
14 Correct 60 ms 14700 KB Output is correct
15 Correct 59 ms 14768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 14012 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 54 ms 13780 KB Output is correct
4 Correct 80 ms 15352 KB Output is correct
5 Correct 72 ms 15284 KB Output is correct
6 Correct 56 ms 13932 KB Output is correct
7 Correct 60 ms 13956 KB Output is correct
8 Correct 66 ms 14556 KB Output is correct
9 Correct 66 ms 14620 KB Output is correct
10 Correct 63 ms 14412 KB Output is correct
11 Correct 56 ms 14404 KB Output is correct
12 Correct 57 ms 14356 KB Output is correct
13 Correct 65 ms 14320 KB Output is correct
14 Correct 60 ms 14700 KB Output is correct
15 Correct 59 ms 14768 KB Output is correct
16 Correct 52 ms 12560 KB Output is correct
17 Correct 51 ms 12552 KB Output is correct
18 Correct 55 ms 12284 KB Output is correct
19 Correct 50 ms 12248 KB Output is correct
20 Correct 55 ms 12784 KB Output is correct
21 Correct 59 ms 12660 KB Output is correct
22 Correct 66 ms 14856 KB Output is correct
23 Correct 57 ms 12304 KB Output is correct
24 Correct 51 ms 12356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 884 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 888 KB Output is correct
4 Correct 4 ms 876 KB Output is correct
5 Correct 2 ms 876 KB Output is correct
6 Correct 2 ms 880 KB Output is correct
7 Correct 3 ms 876 KB Output is correct
8 Correct 3 ms 876 KB Output is correct
9 Correct 2 ms 880 KB Output is correct
10 Correct 3 ms 884 KB Output is correct
11 Correct 2 ms 888 KB Output is correct
12 Correct 2 ms 876 KB Output is correct
13 Correct 3 ms 868 KB Output is correct
14 Correct 2 ms 876 KB Output is correct
15 Correct 2 ms 888 KB Output is correct
16 Correct 3 ms 876 KB Output is correct
17 Correct 2 ms 876 KB Output is correct
18 Correct 3 ms 884 KB Output is correct
19 Correct 2 ms 876 KB Output is correct
20 Correct 3 ms 812 KB Output is correct
21 Correct 2 ms 876 KB Output is correct
22 Correct 2 ms 876 KB Output is correct
23 Correct 3 ms 876 KB Output is correct
24 Correct 2 ms 876 KB Output is correct
25 Correct 3 ms 876 KB Output is correct
26 Correct 3 ms 884 KB Output is correct
27 Correct 3 ms 884 KB Output is correct
28 Correct 2 ms 872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 12180 KB Output is correct
2 Correct 70 ms 13928 KB Output is correct
3 Correct 2 ms 568 KB Output is correct
4 Correct 43 ms 11932 KB Output is correct
5 Correct 62 ms 15976 KB Output is correct
6 Correct 69 ms 15920 KB Output is correct
7 Correct 53 ms 14796 KB Output is correct
8 Correct 57 ms 14812 KB Output is correct
9 Correct 69 ms 15932 KB Output is correct
10 Correct 76 ms 15996 KB Output is correct
11 Correct 62 ms 15944 KB Output is correct
12 Correct 68 ms 15976 KB Output is correct
13 Correct 73 ms 15892 KB Output is correct
14 Correct 62 ms 16040 KB Output is correct
15 Correct 62 ms 15884 KB Output is correct
16 Correct 69 ms 15924 KB Output is correct
17 Correct 68 ms 15588 KB Output is correct
18 Correct 65 ms 15504 KB Output is correct
19 Correct 63 ms 15612 KB Output is correct
20 Correct 67 ms 15508 KB Output is correct
21 Correct 59 ms 15560 KB Output is correct
22 Correct 60 ms 15600 KB Output is correct
23 Correct 64 ms 12288 KB Output is correct
24 Correct 57 ms 12668 KB Output is correct
25 Correct 56 ms 13348 KB Output is correct
26 Correct 59 ms 13348 KB Output is correct
27 Correct 65 ms 14540 KB Output is correct
28 Correct 60 ms 14552 KB Output is correct
29 Correct 67 ms 14608 KB Output is correct
30 Correct 64 ms 14572 KB Output is correct
31 Correct 50 ms 12676 KB Output is correct
32 Correct 56 ms 12672 KB Output is correct
33 Correct 58 ms 13224 KB Output is correct
34 Correct 60 ms 13172 KB Output is correct
35 Correct 56 ms 14320 KB Output is correct
36 Correct 57 ms 14384 KB Output is correct
37 Correct 64 ms 14324 KB Output is correct
38 Correct 64 ms 14352 KB Output is correct
39 Correct 57 ms 14448 KB Output is correct
40 Correct 61 ms 14404 KB Output is correct
41 Correct 57 ms 15256 KB Output is correct
42 Correct 57 ms 15256 KB Output is correct
43 Correct 57 ms 15276 KB Output is correct
44 Correct 68 ms 15316 KB Output is correct
45 Correct 64 ms 15420 KB Output is correct
46 Correct 62 ms 15252 KB Output is correct
47 Correct 55 ms 14076 KB Output is correct
48 Correct 56 ms 14048 KB Output is correct
49 Correct 54 ms 13936 KB Output is correct
50 Correct 55 ms 14232 KB Output is correct
51 Correct 51 ms 12832 KB Output is correct
52 Correct 50 ms 12744 KB Output is correct
53 Correct 56 ms 12836 KB Output is correct
54 Correct 54 ms 12944 KB Output is correct
55 Correct 53 ms 12704 KB Output is correct
56 Correct 54 ms 12748 KB Output is correct
57 Correct 53 ms 12800 KB Output is correct
58 Correct 61 ms 12832 KB Output is correct
59 Correct 56 ms 12684 KB Output is correct
60 Correct 59 ms 12748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 12344 KB Output is correct
2 Correct 54 ms 13644 KB Output is correct
3 Correct 2 ms 496 KB Output is correct
4 Correct 44 ms 11936 KB Output is correct
5 Correct 64 ms 15968 KB Output is correct
6 Correct 74 ms 15856 KB Output is correct
7 Correct 67 ms 15136 KB Output is correct
8 Correct 60 ms 15236 KB Output is correct
9 Correct 63 ms 16424 KB Output is correct
10 Correct 63 ms 16492 KB Output is correct
11 Correct 72 ms 16356 KB Output is correct
12 Correct 66 ms 16336 KB Output is correct
13 Correct 65 ms 16280 KB Output is correct
14 Correct 73 ms 16408 KB Output is correct
15 Correct 74 ms 16416 KB Output is correct
16 Correct 63 ms 16284 KB Output is correct
17 Correct 70 ms 15948 KB Output is correct
18 Correct 77 ms 15980 KB Output is correct
19 Correct 74 ms 16032 KB Output is correct
20 Correct 59 ms 15916 KB Output is correct
21 Correct 70 ms 15992 KB Output is correct
22 Correct 69 ms 15976 KB Output is correct
23 Correct 52 ms 12508 KB Output is correct
24 Correct 50 ms 12584 KB Output is correct
25 Correct 53 ms 13276 KB Output is correct
26 Correct 53 ms 13388 KB Output is correct
27 Correct 57 ms 14588 KB Output is correct
28 Correct 81 ms 14496 KB Output is correct
29 Correct 75 ms 14548 KB Output is correct
30 Correct 65 ms 14624 KB Output is correct
31 Correct 51 ms 12668 KB Output is correct
32 Correct 57 ms 12664 KB Output is correct
33 Correct 59 ms 13268 KB Output is correct
34 Correct 54 ms 13272 KB Output is correct
35 Correct 57 ms 14292 KB Output is correct
36 Correct 56 ms 14460 KB Output is correct
37 Correct 56 ms 14312 KB Output is correct
38 Correct 57 ms 14480 KB Output is correct
39 Correct 69 ms 14356 KB Output is correct
40 Correct 63 ms 14428 KB Output is correct
41 Correct 59 ms 15284 KB Output is correct
42 Correct 67 ms 15192 KB Output is correct
43 Correct 63 ms 15232 KB Output is correct
44 Correct 57 ms 15352 KB Output is correct
45 Correct 60 ms 15392 KB Output is correct
46 Correct 67 ms 15228 KB Output is correct
47 Correct 55 ms 14176 KB Output is correct
48 Correct 71 ms 14172 KB Output is correct
49 Correct 55 ms 13912 KB Output is correct
50 Correct 56 ms 14188 KB Output is correct
51 Correct 53 ms 12844 KB Output is correct
52 Correct 52 ms 12820 KB Output is correct
53 Correct 54 ms 12836 KB Output is correct
54 Correct 58 ms 12864 KB Output is correct
55 Correct 57 ms 12780 KB Output is correct
56 Correct 61 ms 12804 KB Output is correct
57 Correct 57 ms 12788 KB Output is correct
58 Correct 54 ms 12840 KB Output is correct
59 Correct 63 ms 12948 KB Output is correct
60 Correct 56 ms 12768 KB Output is correct