Cod sursa(job #1355596)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 22 februarie 2015 21:24:15
Problema Copii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>

#define DIM 20
#define vint vector<int>::iterator
#define infile "copii.in"
#define outfile "copii.out"

using namespace std;

ifstream f(infile);
ofstream g(outfile);

int n;

int goodConfigs;

char friends[DIM][DIM];

int inGroup[DIM];

bool ok[DIM];

vector<int> groups[DIM];

void back(int k, int groupsCount) {

	if (k == n + 1) {

		if (groupsCount == 1)
			return;

		for (int i = 1; i <= groupsCount; ++i) {

			for (int j = 1; j <= n; ++j)
				ok[j] = false;

			for (vint it = groups[i].begin(); it != groups[i].end(); ++it)
				for (int j = 1; j <= n; ++j)
					if (friends[*it][j] == '1')
						ok[inGroup[j]] = 1;

			for (int j = 1; j <= groupsCount; ++j)
				if (j != i && !ok[j])
					return;

		}

		++goodConfigs;

		return;

	}

	for (int i = 1; i <= groupsCount; ++i) {

		groups[i].push_back(k);

		inGroup[k] = i;

		back(k + 1, groupsCount);

		groups[i].pop_back();

	}

	++groupsCount;

	groups[groupsCount].push_back(k);

	inGroup[k] = groupsCount;

	back(k + 1, groupsCount);

	groups[groupsCount--].pop_back();

}

int main() {

	f >> n;

	for (int i = 1; i <= n; ++i)
		f >> friends[i] + 1;

	back(1, 0);

	g << goodConfigs;

	return 0;
}

//Trust me, I'm the Doctor!