답안 #1022669

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1022669 2024-07-13T22:13:45 Z PurpleCrayon 카멜레온의 사랑 (JOI20_chameleon) C++17
100 / 100
31 ms 656 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 {};
    if (r - l + 1 < 2) {
        vector<pair<int, int>> edges;
        for (int i = l; i <= r; i++)
            for (int j = l; j < i; j++)
                if (Query({i, j}) != 2)
                    edges.emplace_back(i, j);
        return edges;
    }

    int m = (l + r) / 2;
    /*
    if (r - l > 10 && rand() % 10 == 0) {
        m--;
    }
    */
    if (r - l > 10)
        m = l + (r - l) / 3;

    auto edges_one = rec(l, m), edges_two = rec(m + 1, r);
    vector<pair<int, int>> edges;
    for (auto e : edges_one) edges.push_back(e);
    for (auto e : edges_two) edges.push_back(e);

    vector<int> cnt(r + 1);
    for (auto [a, b] : edges)
        cnt[a]++, cnt[b]++;

    auto cols = build_cols(edges_two, m + 1, r);
    vector<int> one, two;
    for (int i = m + 1; i <= r; i++) {
        if (cnt[i] == 3) continue;

        if (cols[i])
            one.push_back(i);
        else
            two.push_back(i);
    }


    for (int i = l; i <= m; i++) if (cnt[i] < 3) {
        for (int rep = 0; rep < 2; rep++) if (cnt[i] < 3) {
            auto check = [&](int L, int R) {
                vector<int> v{i};
                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[i] < 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(i, one[hi]);
                cnt[i]++, 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);
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 16 ms 600 KB Output is correct
4 Correct 16 ms 600 KB Output is correct
5 Correct 16 ms 604 KB Output is correct
6 Correct 16 ms 600 KB Output is correct
7 Correct 16 ms 600 KB Output is correct
8 Correct 16 ms 600 KB Output is correct
9 Correct 16 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 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 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 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 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 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 0 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 0 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
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 25 ms 596 KB Output is correct
4 Correct 25 ms 600 KB Output is correct
5 Correct 31 ms 600 KB Output is correct
6 Correct 25 ms 600 KB Output is correct
7 Correct 25 ms 580 KB Output is correct
8 Correct 25 ms 600 KB Output is correct
9 Correct 27 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 16 ms 600 KB Output is correct
4 Correct 16 ms 600 KB Output is correct
5 Correct 16 ms 604 KB Output is correct
6 Correct 16 ms 600 KB Output is correct
7 Correct 16 ms 600 KB Output is correct
8 Correct 16 ms 600 KB Output is correct
9 Correct 16 ms 600 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 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 1 ms 344 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 1 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 0 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 0 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 25 ms 596 KB Output is correct
31 Correct 25 ms 600 KB Output is correct
32 Correct 31 ms 600 KB Output is correct
33 Correct 25 ms 600 KB Output is correct
34 Correct 25 ms 580 KB Output is correct
35 Correct 25 ms 600 KB Output is correct
36 Correct 27 ms 600 KB Output is correct
37 Correct 23 ms 600 KB Output is correct
38 Correct 16 ms 600 KB Output is correct
39 Correct 23 ms 600 KB Output is correct
40 Correct 23 ms 600 KB Output is correct
41 Correct 26 ms 600 KB Output is correct
42 Correct 15 ms 656 KB Output is correct
43 Correct 25 ms 600 KB Output is correct
44 Correct 24 ms 600 KB Output is correct
45 Correct 23 ms 632 KB Output is correct