Submission #1022671

# Submission time Handle Problem Language Result Execution time Memory
1022671 2024-07-13T22:17:57 Z PurpleCrayon Chameleon's Love (JOI20_chameleon) C++17
100 / 100
38 ms 1120 KB
#include "chameleon.h"
#include <bits/stdc++.h>
using namespace std;

#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 5e2+10, MOD = 1e9+7;

namespace {

}  // namespace

vector<int> adj[2 * N];
vector<int> vis; 

void dfs_col(int c, int b) {
    vis[c] = b;
    for (int nxt : adj[c]) if (vis[nxt] == -1)
        dfs_col(nxt, b ^ 1);
}

vector<int> build_cols(vector<pair<int, int>> edges, int l, int r) {
    vis.assign(r + 1, -1);
    for (int i = l; i <= r; i++)
        adj[i].clear();

    for (auto [a, b] : edges)
        adj[a].push_back(b), adj[b].push_back(a);

    for (int i = l; i <= r; i++) if (vis[i] == -1)
        dfs_col(i, 0);

    return vis;
}

vector<pair<int, int>> rec(int l, int r) {
    if (l >= r) return {};
    auto edges = rec(l + 1, r);
    vector<int> cnt(r + 1);
    for (auto [a, b] : edges)
        cnt[a]++, cnt[b]++;

    auto cols = build_cols(edges, l + 1, r);
    vector<int> one, two;
    for (int i = l + 1; i <= r; i++) {
        if (cnt[i] == 3) continue;
        if (cols[i])
            one.push_back(i);
        else
            two.push_back(i);
    }


    for (int rep = 0; rep < 2; rep++) if (cnt[l] < 3) {
        auto check = [&](int L, int R) {
            vector<int> v{l};
            for (int j = L; j <= R; j++)
                v.push_back(one[j]);

            if (Query(v) != sz(v))
                return true;
            else
                return false;
        };
        int L = 0;
        while (L < sz(one) && cnt[l] < 3) {
            if (!check(L, sz(one)-1))
                break;

            int lo = -1, hi = sz(one)-1, mid = (lo + hi) / 2;
            while (lo < mid && mid < hi) {
                if (check(L, mid)) hi = mid;
                else lo = mid;
                mid = (lo + hi) / 2;
            }

            edges.emplace_back(l, one[hi]);
            cnt[l]++, cnt[one[hi]]++;
            L = hi + 1;
        }

        swap(one, two);
    }

    return edges;
}

void Solve(int n) {
    auto edges = rec(1, 2*n);
    vector<vector<int>> adj(2 * n + 1);
    vector<int> out(2 * n + 1);
    for (auto [a, b] : edges) {
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    for (int i = 1; i <= 2 * n; i++) {
        if (sz(adj[i]) == 1) {
            continue;
        }

        assert(sz(adj[i]) == 3);
        random_shuffle(adj[i].begin(), adj[i].end());
        out[i] = adj[i][2];
        for (int j = 0; j <= 1; j++) if (out[adj[i][j]] != i) {
            vector<int> v{i};
            for (int k = 0; k < 3; k++) if (k != j)
                v.push_back(adj[i][k]);

            if (Query(v) == 1) {
                out[i] = adj[i][j];
                break;
            }
        }
    }

    for (int a = 1; a <= 2 * n; a++) {
        for (int b : adj[a]) {
            if (out[a] == b) continue;
            if (out[b] == a) continue;
            if (a > b) continue;
            Answer(a, b);
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 19 ms 860 KB Output is correct
4 Correct 20 ms 1120 KB Output is correct
5 Correct 26 ms 876 KB Output is correct
6 Correct 19 ms 856 KB Output is correct
7 Correct 19 ms 856 KB Output is correct
8 Correct 23 ms 856 KB Output is correct
9 Correct 19 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 29 ms 860 KB Output is correct
4 Correct 28 ms 872 KB Output is correct
5 Correct 34 ms 856 KB Output is correct
6 Correct 33 ms 856 KB Output is correct
7 Correct 29 ms 884 KB Output is correct
8 Correct 29 ms 856 KB Output is correct
9 Correct 29 ms 872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 19 ms 860 KB Output is correct
4 Correct 20 ms 1120 KB Output is correct
5 Correct 26 ms 876 KB Output is correct
6 Correct 19 ms 856 KB Output is correct
7 Correct 19 ms 856 KB Output is correct
8 Correct 23 ms 856 KB Output is correct
9 Correct 19 ms 856 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 0 ms 344 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 29 ms 860 KB Output is correct
31 Correct 28 ms 872 KB Output is correct
32 Correct 34 ms 856 KB Output is correct
33 Correct 33 ms 856 KB Output is correct
34 Correct 29 ms 884 KB Output is correct
35 Correct 29 ms 856 KB Output is correct
36 Correct 29 ms 872 KB Output is correct
37 Correct 35 ms 856 KB Output is correct
38 Correct 19 ms 856 KB Output is correct
39 Correct 30 ms 856 KB Output is correct
40 Correct 32 ms 880 KB Output is correct
41 Correct 30 ms 856 KB Output is correct
42 Correct 19 ms 856 KB Output is correct
43 Correct 29 ms 856 KB Output is correct
44 Correct 30 ms 900 KB Output is correct
45 Correct 38 ms 868 KB Output is correct