Cod sursa(job #2475883)

Utilizator ValentinSavoiuFMI Savoiu Valentin-Marian ValentinSavoiu Data 17 octombrie 2019 18:43:20
Problema Copii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f ("copii.in");
ofstream g ("copii.out"); 
int groups[10];
int N;
int cnt = 0;
vector <int> V;
void check(int nr_groups) {
    if (nr_groups <= 1)
        return;
    vector <int>  friends(nr_groups, 0);
    for (int i = 0; i < nr_groups; ++i) {
        for (int j = 0; j < N; ++j) {
            if ((1 << j) & groups[i]) 
                friends[i] |= V[j];
        }
    }    
    for (int i = 0; i < nr_groups; ++i)
        for (int j = 0; j < nr_groups; ++j) {
            if (i == j) continue;
            if ((friends[i] & groups[j]) == 0)
                return;
        } 
    ++cnt;
}

void bkt (int LVL, int curr_groups) {
    if (LVL == N) {
        check(curr_groups);
        return;
    }
    for (int i = 0; i < curr_groups; ++i) {
        groups[i] ^= (1 << (LVL));
        bkt(LVL + 1, curr_groups);
        groups[i] ^= (1 << (LVL));
    }
    groups[curr_groups] ^= (1 << LVL);
    bkt(LVL + 1, curr_groups + 1);
    groups[curr_groups] ^= (1 << LVL);
}

int main() {
    f >> N;
    
    for (int i = 0; i < N; ++i) {
        int x = 0;
        string s;
        f >> s;
        for (int j = 0; j < N; ++j) {
            x |= ((s[j] - '0')* (1 << j));
        }
        x |= (1 << i);
        V.push_back(x);
    }
    bkt(0, 0);
    g << cnt;
}