답안 #134589

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
134589 2019-07-23T04:53:02 Z imeimi2000 Friends (BOI17_friends) C++17
100 / 100
343 ms 11104 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <random>
#include <cstdlib>

using namespace std;
typedef long long llong;

void fail() {
    printf("detention\n");
    exit(0);
}

int n, p, q;
vector<int> edge[2500];
int con[2500][2500];
vector<int> group[2500];

bool in[2500];
bool ex[2500];
bool backtrack(vector<int> &ret, int it = 0, int jt = 0, int np = 1, int nq = 0) {
    if (p < np || q < nq) return 0;
    while (it < ret.size()) {
        while (jt < edge[ret[it]].size()) {
            int x = edge[ret[it]][jt++];
            if (!in[x] && !ex[x]) {
                {
                    int nxp = np, nxq = nq;
                    in[x] = 1;
                    ret.push_back(x);
                    ++nxp;
                    for (int i : edge[x]) {
                        if (ex[i]) ++nxq;
                    }
                    if (backtrack(ret, it, jt, nxp, nxq))
                        return 1;
                    in[x] = 0;
                    ret.pop_back();
                }
                {
                    int nxp = np, nxq = nq;
                    ex[x] = 1;
                    for (int i : edge[x]) {
                        if (in[i]) ++nxq;
                    }
                    if (backtrack(ret, it, jt, nxp, nxq))
                        return 1;
                    ex[x] = 0;
                }
                return 0;
            }
        }
        ++it;
        jt = 0;
    }
    return 1;
}

void pre_process() {
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j)
            in[j] = ex[j] = 0;
        in[i] = 1;
        group[i].push_back(i);
        if (!backtrack(group[i])) fail();
    }
}

int st[2500];
void solve() {
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < i; ++j) {
            for (int k : group[i]) st[k] |= 1;
            vector<int> gs;
            for (int k : group[j]) {
                st[k] |= 2;
                if (st[k] == 3) gs.push_back(k);
            }
            int c1 = 0, c2 = 0;
            for (int k : gs) {
                for (int l : edge[k]) {
                    if (st[l] == 1) ++c1;
                    if (st[l] == 2) ++c2;
                }
            }
            if (c1 < c2) {
                vector<int> ns;
                for (int k : group[i]) if (st[k] == 1)
                    ns.push_back(k);
                group[i] = ns;
            }
            else {
                vector<int> ns;
                for (int k : group[j]) if (st[k] == 2)
                    ns.push_back(k);
                group[j] = ns;
            }
            for (int k : group[i]) st[k] = 0;
            for (int k : group[j]) st[k] = 0;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> p >> q;
    {
        int one_cnt = 0;
        for (int i = 0; i < n; ++i) {
            int k;
            cin >> k;
            while (k--) {
                int x;
                cin >> x;
                if (con[i][x] == 1) --one_cnt;
                if (++con[i][x] == 1) ++one_cnt;
                ++con[x][i];
                edge[i].push_back(x);
            }
        }
        if (one_cnt > 0) fail();
    }
    for (int i = 0; i < n; ++i) {
        if (p + q <= edge[i].size()) fail();
        shuffle(edge[i].begin(), edge[i].end(), mt19937(time(0)));
    }
    pre_process();
    solve();
    printf("home\n");
    int cnt = 0;
    for (int i = 0; i < n; ++i) if (!group[i].empty())
        ++cnt;
    printf("%d\n", cnt);
    for (int i = 0; i < n; ++i) {
        if (group[i].empty()) continue;
        sort(group[i].begin(), group[i].end());
        printf("%u", group[i].size());
        for (int j : group[i]) printf(" %d", j);
        printf("\n");
    }
    return 0;
}

Compilation message

friends.cpp: In function 'bool backtrack(std::vector<int>&, int, int, int, int)':
friends.cpp:25:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while (it < ret.size()) {
            ~~~^~~~~~~~~~~~
friends.cpp:26:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while (jt < edge[ret[it]].size()) {
                ~~~^~~~~~~~~~~~~~~~~~~~~~
friends.cpp: In function 'int main()':
friends.cpp:126:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (p + q <= edge[i].size()) fail();
             ~~~~~~^~~~~~~~~~~~~~~~~
friends.cpp:139:37: warning: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'std::vector<int>::size_type {aka long unsigned int}' [-Wformat=]
         printf("%u", group[i].size());
                      ~~~~~~~~~~~~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 632 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 6 ms 1272 KB Output is correct
3 Correct 5 ms 1144 KB Output is correct
4 Correct 4 ms 1528 KB Output is correct
5 Correct 5 ms 1528 KB Output is correct
6 Correct 4 ms 1528 KB Output is correct
7 Correct 4 ms 888 KB Output is correct
8 Correct 21 ms 1528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 6 ms 1272 KB Output is correct
3 Correct 5 ms 1144 KB Output is correct
4 Correct 4 ms 1528 KB Output is correct
5 Correct 5 ms 1528 KB Output is correct
6 Correct 4 ms 1528 KB Output is correct
7 Correct 4 ms 888 KB Output is correct
8 Correct 21 ms 1528 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 63 ms 4728 KB Output is correct
11 Correct 85 ms 5756 KB Output is correct
12 Correct 85 ms 5580 KB Output is correct
13 Correct 92 ms 5752 KB Output is correct
14 Correct 18 ms 7928 KB Output is correct
15 Correct 9 ms 7928 KB Output is correct
16 Correct 10 ms 8696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 632 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 6 ms 1272 KB Output is correct
11 Correct 5 ms 1144 KB Output is correct
12 Correct 4 ms 1528 KB Output is correct
13 Correct 5 ms 1528 KB Output is correct
14 Correct 4 ms 1528 KB Output is correct
15 Correct 4 ms 888 KB Output is correct
16 Correct 21 ms 1528 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 63 ms 4728 KB Output is correct
19 Correct 85 ms 5756 KB Output is correct
20 Correct 85 ms 5580 KB Output is correct
21 Correct 92 ms 5752 KB Output is correct
22 Correct 18 ms 7928 KB Output is correct
23 Correct 9 ms 7928 KB Output is correct
24 Correct 10 ms 8696 KB Output is correct
25 Correct 2 ms 504 KB Output is correct
26 Correct 178 ms 8516 KB Output is correct
27 Correct 159 ms 8060 KB Output is correct
28 Correct 137 ms 7548 KB Output is correct
29 Correct 13 ms 4856 KB Output is correct
30 Correct 17 ms 5880 KB Output is correct
31 Correct 14 ms 5880 KB Output is correct
32 Correct 10 ms 8700 KB Output is correct
33 Correct 343 ms 11104 KB Output is correct
34 Correct 28 ms 10872 KB Output is correct