Cod sursa(job #1857014)

Utilizator contnouAndrei Pavel contnou Data 25 ianuarie 2017 18:36:05
Problema Copii Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.92 kb
#include <fstream>
#include <string.h>
#define MAXK 13
using namespace std;

ifstream f("copii.in");
ofstream g("copii.out");

int childCount;
char **children = 0;
int teams;
int n;
int assignedGroup[MAXK], prevmax[MAXK], countTeams = 0;

void readData() {
	//
	f >> childCount;

	children = new char*[childCount + 1];
	for (int i = 0; i < childCount; i++) {
		children[i] = new char[childCount + 1];
		f >> children[i];
	}

	return;
}

bool isSolution() {
	//
	int groups[MAXK][MAXK], distinctGroups[MAXK];
	int groupsCount = 0, countRelations = 0;

	memset(groups, 0, MAXK * MAXK * sizeof(int));
	memset(distinctGroups, 0, MAXK * sizeof(int));

	for (int i = 1; i <= childCount; i++) {
		groupsCount += (distinctGroups[assignedGroup[i]] == 0 ? 1 : 0);
		distinctGroups[assignedGroup[i]] = 1;
	}

	if (groupsCount < 2) return false;

	for (int i = 1; i <= childCount; i++) {
		for (int j = 1; j <= childCount; j++) {
			if (i != j && children[i - 1][j - 1] == '1' && assignedGroup[i] != assignedGroup[j]) {
				//
				if (!groups[assignedGroup[i]][assignedGroup[j]]) countRelations++;
				groups[assignedGroup[i]][assignedGroup[j]] = 1;
			}
		}
	}

	return (countRelations == (groupsCount * groupsCount - groupsCount));
}

int getMax(int a, int b) {
	//
	return a > b ? a : b;
}

void combinations(int pos, int &countTeams) {
	//
	for (int i = 0; i <= prevmax[pos - 1] + 1; i++) {
		//
		assignedGroup[pos] = i;
		prevmax[pos] = getMax(prevmax[pos - 1], i);

		if (pos < childCount) {
			combinations(pos + 1, countTeams);
		} else if (isSolution()) {
			countTeams++;
		}
	}
}

int teamUp() {
	//
	
	memset(assignedGroup, 0, MAXK * sizeof(int));
	memset(prevmax, 0, MAXK * sizeof(int));

	assignedGroup[0] = -1;
	prevmax[0] = -1;

	combinations(1, countTeams);

	return countTeams;
}

int main() {
	//
	readData();
	teams = teamUp();
	g << teams;

	return 0;
}