Combinations

Please help me to solve this:

There are n students who can be good in d domains. Determine the groups of k students that will cover all the d domains.

The input file pluricex.in contains the three integer numbers, n, k and d. Each of the following n lines contains an integer number nr that represents the number of domains which a student is good in and nr integer numbers that represent the domains.

The output file pluricex.out contains all the groups that can be formed. The members will be written in ascending order. The groups will be written in lexicographic order.

0 < n ≤ 22
0 < k ≤ 8
0 < d ≤ 10
There will always be a solution. The number of solutions is <2000

e.g.

pluricex.in
6 3 5
1 2
2 1 4
3 2 4 3
1 5
2 3 1
1 3
pluricex.out
2 3 4
3 4 5


This is what I did, but I get only 80 out of 100 points.

using namespace std;
#include <fstream>
FILE *f=fopen ("pluricex.in", "r");
ofstream g ("pluricex.out");

int n,k,d, m[24][12],v[12],x[23];

void afisare_sol ()
{
int i,j,v[12]={0},ok=1;
for (i=1; i<=k; i++)
{
for (j=1; j<=m[x[i]][0]; j++) v[m[x[i]][j]]=1;
}
for (i=1; i<=d && ok==1; i++)
{
if (v[i]==0) ok=0;
}
if (ok==1)
{
for (i=1; i<=k; i++) g<<x[i]<<" ";
g<<'\n';
}
}
void comb (int l)
{
int i;
if(l==k+1)
afisare_sol();
else
for(i=x[l-1]+1;i<=n-k+l;i++)
{
x[l]=i;
comb(l+1);
}
}

int main ()
{
int i,j;

fscanf (f, "%d%d%d",&n,&k,&d);
for (i=1; i<=n; i++)
{
fscanf (f,"%d", &m[i][0]);

for (j=1; j<=m[i][0];j++) fscanf (f, "%d", &m[i][j]);
}
comb (1);

fclose (f);
g.close ();
return 0;
}
Last edited on
Topic archived. No new replies allowed.