답안 #646564

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
646564 2022-09-30T05:28:48 Z Cyanmond Friends (BOI17_friends) C++17
100 / 100
568 ms 1712 KB
#include <bits/stdc++.h>

int main() {
    int N, P, Q;
    std::cin >> N >> P >> Q;

    std::vector<int> M(N);
    std::vector<std::vector<int>> graph(N);
    for (int i = 0; i < N; ++i) {
        std::cin >> M[i];
        graph[i].resize(M[i]);
        for (int &e : graph[i]) {
            std::cin >> e;
        }
    }

    // check : input is valid ?
    {
        std::set<std::pair<int, int>> s;
        for (int i = 0; i < N; ++i) {
            for (const int e : graph[i]) {
                const auto pair = std::minmax(i, e);
                if (s.find(pair) == s.end()) {
                    s.insert(pair);
                } else {
                    s.erase(pair);
                }
            }
        }

        if (not s.empty()) {
            std::cout << "detention" << std::endl;
            return 0;
        }
    }

    std::vector<std::set<int>> group(N);
    for (int c = 0; c < N; ++c) {
        auto find = [&](auto &&self, std::set<int> set, std::map<int, int> que, const int r) -> void {
            if ((int)set.size() > P) return;
            if (r > Q) return;
            if ((int)set.size() + r + (int)que.size() > P + Q) return;

            if (que.empty()) {
                if (set.empty()) {
                    return;
                }
                if (group[c].empty() or group[c].size() > set.size()) {
                    group[c] = set;
                }
                return;
            }

            const int from = que.begin() -> first;
            --(que.begin() -> second);
            bool h = true;
            if (que.begin() -> second == 0) {
                h = false;
                que.erase(que.begin());
            }
            self(self, set, que, r + 1);
            
            if ((not group[c].empty()) and group[c].size() <= set.size()) {
                return;
            }
            if (h) que.erase(que.begin());
            set.insert(from);
            for (const int to : graph[from]) {
                if (set.find(to) == set.end()) {
                    ++que[to];
                }
            }
            self(self, set, que, r);
        };
        find(find, {}, {{c, 1}}, 0);
    }

    if (std::any_of(group.begin(), group.end(), [&](const auto &e) { return e.empty(); })) {
        std::cout << "detention" << std::endl;
        return 0;
    }

    std::vector<int> answer_index(N, -1);
    for (int i = 0; i < N; ++i) {
        for (const int e : group[i]) {
            int &x = answer_index[e];
            if (x == -1 or group[x].size() < group[i].size()) {
                x = i;
            }
        }
    }

    std::set<int> ids;
    for (const int e : answer_index) {
        ids.insert(e);
    }
    const int K = (int)ids.size();

    std::vector<std::set<int>> sets;
    for (const int i : ids) {
        sets.push_back(group[i]);
    }

    auto judge = [&](const std::set<int> &s) -> bool {
        int cnt = 0;
        for (const int e : s) {
            for (const int to : graph[e]) {
                if (s.find(to) == s.end()) {
                    ++cnt;
                }
            }
        }
        return cnt <= Q;
    };

    for (int i = 0; i < K; ++i) {
        for (int j = i + 1; j < K; ++j) {
            auto &set_a = sets[i], &set_b = sets[j];

            auto itr_a = set_a.begin(), itr_b = set_b.begin();
            std::vector<int> common_elements;
            while (itr_a != set_a.end() and itr_b != set_b.end()) {
                if (*itr_a == *itr_b) {
                    common_elements.push_back(*itr_a);
                }
                if (*itr_a <= *itr_b) {
                    ++itr_a;
                } else {
                    ++itr_b;
                }
            }
            if (common_elements.empty()) {
                continue;
            }

            auto set_c = set_a;
            for (const int e : common_elements) {
                set_c.erase(e);
            }
            if (judge(set_c)) {
                set_a = set_c;
                continue;
            }

            set_c = set_b;
            for (const int e : common_elements) {
                set_c.erase(e);
            }
            if (judge(set_c)) {
                set_b = set_c;
                continue;
            }
        }
    }

    std::vector<std::set<int>> answer;
    for (const auto &e : sets) {
        if (e.empty()) {
            continue;
        }
        answer.push_back(e);
    }

    std::cout << "home" << std::endl;
    std::cout << answer.size() << std::endl;
    for (const auto &g : answer) {
        std::cout << g.size();
        for (const int e : g) {
            std::cout << ' ' << e;
        }
        std::cout << std::endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 7 ms 376 KB Output is correct
3 Correct 2 ms 300 KB Output is correct
4 Correct 5 ms 340 KB Output is correct
5 Correct 6 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 7 ms 432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 7 ms 376 KB Output is correct
3 Correct 2 ms 300 KB Output is correct
4 Correct 5 ms 340 KB Output is correct
5 Correct 6 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 7 ms 432 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 12 ms 980 KB Output is correct
11 Correct 23 ms 972 KB Output is correct
12 Correct 53 ms 1060 KB Output is correct
13 Correct 50 ms 1076 KB Output is correct
14 Correct 28 ms 916 KB Output is correct
15 Correct 43 ms 984 KB Output is correct
16 Correct 4 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 7 ms 376 KB Output is correct
11 Correct 2 ms 300 KB Output is correct
12 Correct 5 ms 340 KB Output is correct
13 Correct 6 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 7 ms 432 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 12 ms 980 KB Output is correct
19 Correct 23 ms 972 KB Output is correct
20 Correct 53 ms 1060 KB Output is correct
21 Correct 50 ms 1076 KB Output is correct
22 Correct 28 ms 916 KB Output is correct
23 Correct 43 ms 984 KB Output is correct
24 Correct 4 ms 340 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 40 ms 1032 KB Output is correct
27 Correct 20 ms 980 KB Output is correct
28 Correct 51 ms 944 KB Output is correct
29 Correct 346 ms 1144 KB Output is correct
30 Correct 46 ms 584 KB Output is correct
31 Correct 175 ms 872 KB Output is correct
32 Correct 5 ms 436 KB Output is correct
33 Correct 564 ms 1712 KB Output is correct
34 Correct 568 ms 1380 KB Output is correct