답안 #1088458

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1088458 2024-09-14T12:46:01 Z gyg Friends (BOI17_friends) C++17
100 / 100
952 ms 2776 KB
#pragma GCC optimize("Ofast", "unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define arr array
#define vct vector
#define hset unordered_set
#define pii pair<int, int>
const int MAX_N = 2500 + 5;

int n, p, q;
arr<hset<int>, MAX_N> adj;

bool vld(hset<int>& x) {
    if (x.size() > p) return false;
    int cnt = 0;
    for (int u : x) {
        for (int v : adj[u]) {
            cnt += (!x.count(v));
            if (cnt > q) return false;
        }
    }
    return true;
}

bool chck() {
    for (int u = 0; u < n; u++) 
        for (int v : adj[u])
            if (!adj[v].count(u)) return true;
    return false;
}

int cnt;
hset<int> inc, exc, ngh;
arr<hset<int>, MAX_N> grp;
void bcktr(int u) {
    if (grp[u].size()) return;
    if (inc.size() > p) return;
    if (cnt > q) return;
    if (ngh.size() + inc.size() + cnt > p + q) return;
    if (ngh.empty()) { grp[u] = inc; return; }

    int v = *ngh.begin();
    
    inc.insert(v), ngh.erase(v);
    vct<int> ins;
    for (int w : adj[v])    
        if (!(inc.count(w) || exc.count(w) || ngh.count(w))) ins.push_back(w), ngh.insert(w);
    int ch_cnt1 = 0;
    for (int w : exc) ch_cnt1 += (adj[v].count(w));
    cnt += ch_cnt1;
    bcktr(u);
    inc.erase(v), ngh.insert(v);
    for (int w : ins) ngh.erase(w);
    cnt -= ch_cnt1;

    if (grp[u].size()) return;

    int ch_cnt2 = 0;
    for (int w : inc) ch_cnt2 += (adj[w].count(v));
    cnt += ch_cnt2;
    ngh.erase(v), exc.insert(v);
    bcktr(u);
    cnt -= ch_cnt2;
    ngh.insert(v), exc.erase(v);
}
arr<vct<pii>, MAX_N> ext;
bool prcmp() {
    for (int u = 0; u < n; u++) {
        cnt = 0, inc = {u}, exc.clear(), ngh = adj[u];
        bcktr(u);
        if (grp[u].empty()) return true;
        for (int v : grp[u])
            for (int w : adj[v])
                if (!grp[u].count(w)) ext[u].push_back({v, w});
    }
    return false;
}

hset<int> mns(hset<int>& x, hset<int>& y) {
    hset<int> ans;
    for (int z : x)
        if (!y.count(z)) ans.insert(z);
    return ans;
}

void rslv(int u, int v) {
    hset<int> umv = mns(grp[u], grp[v]), vmu = mns(grp[v], grp[u]);
    if (grp[u] == umv) return;
  
    int i = 0, j = 0, k = 0, l = 0;
    for (pii& x : ext[u]) {
        if (umv.count(x.first)) i++;
        else if (grp[v].count(x.second)) j++;
    }
    for (pii& x : ext[v]) {
        if (vmu.count(x.first)) k++;
        else if (grp[u].count(x.second)) l++;
    }
    
    if (i + l <= q) grp[u] = umv;
    else grp[v] = vmu;
}

int main() {
    // freopen("fr.in", "r", stdin);
    cin >> n >> p >> q;
    for (int u = 0; u < n; u++) {
        int x; cin >> x;
        for (int i = 1; i <= x; i++) {
            int v; cin >> v;
            adj[u].insert(v);
        }
    }

    if (chck()) { cout << "detention" << endl; exit(0); }
    if (prcmp()) { cout << "detention" << endl; exit(0); }
    
    for (int u = 0; u < n; u++) {
        for (int v = u + 1; v < n; v++) {
            if (u == v) continue;
            rslv(u, v);
        }
    }

    vct<hset<int>> ans;
    for (int u = 0; u < n; u++)
        if (!grp[u].empty()) ans.push_back(grp[u]);

    cout << "home" << endl;
    cout << ans.size() << endl;
    for (hset<int> x : ans) {
        cout << x.size() << " ";
        for (int y : x) cout << y << " ";
        cout << endl;
    }
}

Compilation message

friends.cpp: In function 'bool vld(std::unordered_set<int>&)':
friends.cpp:15:18: warning: comparison of integer expressions of different signedness: 'std::unordered_set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   15 |     if (x.size() > p) return false;
      |         ~~~~~~~~~^~~
friends.cpp: In function 'void bcktr(int)':
friends.cpp:38:20: warning: comparison of integer expressions of different signedness: 'std::unordered_set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   38 |     if (inc.size() > p) return;
      |         ~~~~~~~~~~~^~~
friends.cpp:40:39: warning: comparison of integer expressions of different signedness: 'std::unordered_set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   40 |     if (ngh.size() + inc.size() + cnt > p + q) return;
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 10 ms 860 KB Output is correct
3 Correct 3 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 13 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 10 ms 860 KB Output is correct
3 Correct 3 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 13 ms 860 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 269 ms 1368 KB Output is correct
11 Correct 221 ms 1624 KB Output is correct
12 Correct 221 ms 1628 KB Output is correct
13 Correct 244 ms 1684 KB Output is correct
14 Correct 3 ms 1116 KB Output is correct
15 Correct 3 ms 1116 KB Output is correct
16 Correct 2 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 10 ms 860 KB Output is correct
11 Correct 3 ms 860 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 13 ms 860 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 269 ms 1368 KB Output is correct
19 Correct 221 ms 1624 KB Output is correct
20 Correct 221 ms 1628 KB Output is correct
21 Correct 244 ms 1684 KB Output is correct
22 Correct 3 ms 1116 KB Output is correct
23 Correct 3 ms 1116 KB Output is correct
24 Correct 2 ms 1116 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 391 ms 1976 KB Output is correct
27 Correct 328 ms 1884 KB Output is correct
28 Correct 295 ms 1872 KB Output is correct
29 Correct 5 ms 1116 KB Output is correct
30 Correct 8 ms 1388 KB Output is correct
31 Correct 3 ms 1112 KB Output is correct
32 Correct 3 ms 1284 KB Output is correct
33 Correct 952 ms 2776 KB Output is correct
34 Correct 9 ms 1892 KB Output is correct