답안 #373643

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
373643 2021-03-05T10:58:27 Z KoD 카멜레온의 사랑 (JOI20_chameleon) C++17
100 / 100
64 ms 768 KB
#include <bits/stdc++.h>
#include "chameleon.h"

template <class T>
using Vec = std::vector<T>;

template <class F>
struct RecLambda: private F {
    explicit RecLambda(F &&f): F(std::forward<F>(f)) { }
    template <class... Args>
    decltype(auto) operator () (Args&&... args) const {
        return F::operator()(*this, std::forward<Args>(args)...);
    }
};

void Solve(int N) {
    Vec<int> color(2 * N + 1);
    Vec<Vec<int>> graph(2 * N + 1);

    const auto difference = [&](const int src, Vec<int> vec) {
        vec.push_back(src);
        return (int) vec.size() - Query(vec);
    };

    const auto enum_edges = RecLambda([&](const auto enum_edges, const int src, Vec<int> partner, int dif) -> int {
        if (graph[src].size() == 3) {
            return 0;
        }
        if (dif == -1) {
            dif = difference(src, partner);
        }
        if (dif == 0) {
            return 0;
        }
        if (partner.size() == 1) {
            return partner.front();
        }
        const auto mid = (int) partner.size() / 2;
        Vec<int> left(partner.begin(), partner.begin() + mid);
        Vec<int> right(partner.begin() + mid, partner.end());
        const auto left_dif = difference(src, left);
        if (left_dif == 0) {
            return enum_edges(src, right, dif);
        }
        else {
            return enum_edges(src, left, left_dif);
        }
    });

    const auto split = RecLambda([&](const auto split, const int u, const int c) -> void {
        if (color[u] != 0) {
            return;
        }
        color[u] = c;
        for (const auto v: graph[u]) {
            split(v, -c);
        }
    });

    for (int src = 1; src <= 2 * N; ++src) {
        Vec<int> v1, v2;
        for (int v = 1; v < src; ++v) {
            bool ok = true;
            for (const auto x: graph[src]) {
                if (x == v) {
                    ok = false;
                    break;
                }
            }
            if (ok) {
                (color[v] == 1 ? v1 : v2).push_back(v);
            }
        }
        while (true) {
            const auto dst = enum_edges(src, v1, -1);
            if (dst == 0) {
                break;
            }
            v1.erase(std::lower_bound(v1.begin(), v1.end(), dst));
            graph[src].push_back(dst);
            graph[dst].push_back(src);
        }
        while (true) {
            const auto dst = enum_edges(src, v2, -1);
            if (dst == 0) {
                break;
            }
            v2.erase(std::lower_bound(v2.begin(), v2.end(), dst));
            graph[src].push_back(dst);
            graph[dst].push_back(src);
        }
        std::fill(color.begin(), color.end(), 0);
        for (int v = 1; v <= src; ++v) {
            if (color[v] == 0) {
                split(v, 1);
            }
        }
    }

    std::set<std::pair<int, int>> good;

    const auto add_good = [&](const int src, const int dst) {
        if (good.find(std::make_pair(dst, src)) != good.end()) {
            Answer(src, dst);
        }
        good.emplace(src, dst);
    };

    for (int src = 1; src <= 2 * N; ++src) {
        if (graph[src].size() == 1) {
            add_good(src, graph[src].front());
        }
        else {
            for (int ng = 0; ng < 3; ++ng) {
                const auto check = graph[src][ng];
                Vec<int> ask;
                for (const auto dst: graph[src]) {
                    if (check != dst) {
                        ask.push_back(dst);
                    }
                }
                if (difference(src, ask) == 1) {
                    add_good(src, check);
                }
            }
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 27 ms 620 KB Output is correct
4 Correct 32 ms 620 KB Output is correct
5 Correct 27 ms 768 KB Output is correct
6 Correct 27 ms 620 KB Output is correct
7 Correct 27 ms 620 KB Output is correct
8 Correct 27 ms 620 KB Output is correct
9 Correct 28 ms 620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 2 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 2 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 48 ms 620 KB Output is correct
4 Correct 49 ms 620 KB Output is correct
5 Correct 47 ms 620 KB Output is correct
6 Correct 64 ms 636 KB Output is correct
7 Correct 58 ms 620 KB Output is correct
8 Correct 48 ms 620 KB Output is correct
9 Correct 48 ms 612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 27 ms 620 KB Output is correct
4 Correct 32 ms 620 KB Output is correct
5 Correct 27 ms 768 KB Output is correct
6 Correct 27 ms 620 KB Output is correct
7 Correct 27 ms 620 KB Output is correct
8 Correct 27 ms 620 KB Output is correct
9 Correct 28 ms 620 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 2 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 2 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 0 ms 364 KB Output is correct
29 Correct 0 ms 364 KB Output is correct
30 Correct 48 ms 620 KB Output is correct
31 Correct 49 ms 620 KB Output is correct
32 Correct 47 ms 620 KB Output is correct
33 Correct 64 ms 636 KB Output is correct
34 Correct 58 ms 620 KB Output is correct
35 Correct 48 ms 620 KB Output is correct
36 Correct 48 ms 612 KB Output is correct
37 Correct 56 ms 548 KB Output is correct
38 Correct 28 ms 620 KB Output is correct
39 Correct 63 ms 748 KB Output is correct
40 Correct 51 ms 620 KB Output is correct
41 Correct 50 ms 620 KB Output is correct
42 Correct 27 ms 620 KB Output is correct
43 Correct 48 ms 620 KB Output is correct
44 Correct 51 ms 620 KB Output is correct
45 Correct 56 ms 620 KB Output is correct