Cod sursa(job #162594)

Utilizator luana_0105Fagarasan Luana luana_0105 Data 20 martie 2008 12:45:03
Problema Dusman Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include<stdio.h>
#define nmax 1005

int d[nmax][nmax], s[nmax];
int n,m,k,ct;

void read()
{
     int i,n1,n2;
     freopen("dusman.in","r",stdin);
     freopen("dusman.out","w",stdout);
     scanf("%d%d%d",&n, &k, &m);
     for (i=1; i<=m; ++i)
     {
         scanf("%d%d", &n1, &n2);
         d[n1][n2]=1;
         d[n2][n1]=1;
     }
}

int valid(int p)
{
    for (int i=1; i<p; ++i)
       if(s[i]==s[p])
         return 0;
    
    if(d[s[p-1]][s[p]]==1)
      return 0;
    return 1;
}

void print()
{
     for(int i=1; i<=n; i++)
        printf("%d ", s[i]);
      printf("\n");
}
    
    

void bec(int p)
{
     for(int i=s[p-1]+1; i<=n; ++i)
     {
         s[p]=i;
         if(valid(p)==1)
            if(p==n)
            {
               ct++;
               if(ct==k)
               {
                   print();
                   return;
               }
            }
            else
                bec(p+1);
     }
}

int main()
{
    read();
    bec(1);
    return 0;
}