답안 #133356

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
133356 2019-07-20T12:33:11 Z imeimi2000 Friends (BOI17_friends) C++17
100 / 100
340 ms 11236 KB
#include <iostream>
#include <algorithm>
#include <vector>
#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();
    }
    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:23:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while (it < ret.size()) {
            ~~~^~~~~~~~~~~~
friends.cpp:24: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:124:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (p + q <= edge[i].size()) fail();
             ~~~~~~^~~~~~~~~~~~~~~~~
friends.cpp:136: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 2 ms 504 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 508 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 5 ms 1272 KB Output is correct
3 Correct 4 ms 1144 KB Output is correct
4 Correct 3 ms 1528 KB Output is correct
5 Correct 3 ms 1528 KB Output is correct
6 Correct 3 ms 1528 KB Output is correct
7 Correct 3 ms 888 KB Output is correct
8 Correct 7 ms 1528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 5 ms 1272 KB Output is correct
3 Correct 4 ms 1144 KB Output is correct
4 Correct 3 ms 1528 KB Output is correct
5 Correct 3 ms 1528 KB Output is correct
6 Correct 3 ms 1528 KB Output is correct
7 Correct 3 ms 888 KB Output is correct
8 Correct 7 ms 1528 KB Output is correct
9 Correct 2 ms 508 KB Output is correct
10 Correct 59 ms 4728 KB Output is correct
11 Correct 79 ms 5760 KB Output is correct
12 Correct 80 ms 5576 KB Output is correct
13 Correct 87 ms 5880 KB Output is correct
14 Correct 10 ms 7928 KB Output is correct
15 Correct 12 ms 7928 KB Output is correct
16 Correct 10 ms 8696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 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 508 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 5 ms 1272 KB Output is correct
11 Correct 4 ms 1144 KB Output is correct
12 Correct 3 ms 1528 KB Output is correct
13 Correct 3 ms 1528 KB Output is correct
14 Correct 3 ms 1528 KB Output is correct
15 Correct 3 ms 888 KB Output is correct
16 Correct 7 ms 1528 KB Output is correct
17 Correct 2 ms 508 KB Output is correct
18 Correct 59 ms 4728 KB Output is correct
19 Correct 79 ms 5760 KB Output is correct
20 Correct 80 ms 5576 KB Output is correct
21 Correct 87 ms 5880 KB Output is correct
22 Correct 10 ms 7928 KB Output is correct
23 Correct 12 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 177 ms 8484 KB Output is correct
27 Correct 151 ms 8084 KB Output is correct
28 Correct 131 ms 7464 KB Output is correct
29 Correct 8 ms 4856 KB Output is correct
30 Correct 11 ms 5880 KB Output is correct
31 Correct 8 ms 5880 KB Output is correct
32 Correct 10 ms 8748 KB Output is correct
33 Correct 340 ms 11236 KB Output is correct
34 Correct 17 ms 10872 KB Output is correct