Pagini recente » Cod sursa (job #3191419) | Cod sursa (job #1281767) | Cod sursa (job #2533050) | Cod sursa (job #1119032) | Cod sursa (job #1857014)
#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;
}