Cod sursa(job #422197)

Utilizator AndreiRSStatescu Andrei Rares AndreiRS Data 22 martie 2010 12:18:25
Problema Dusman Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <stdio.h>
#include <stdlib.h>

int n,perm,m;
int a[1001][1001];
int viz[1001];
int v[1001];

FILE *f1 = fopen("dusman.in","r");
FILE *f2 = fopen("dusman.out","w");

int cont(int k){
	if(k > 1 && a[v[k]][v[k-1]])
		return 0;
	return 1;	
}

void back(int k){
	int i;
	
	if(k == n + 1){
		perm--;
		if(!perm){
			for(i=1; i<=n; i++)
				fprintf(f2,"%d ",v[i]);	
			fclose(f1);
			fclose(f2);
			exit(0);
			
		}
		return;
	}
	for(i=1; i<=n; i++){
		if(!viz[i]){
			v[k] = i;
			viz[i] = 1;
			if(cont(k))
				back(k+1);
			viz[i] = 0;
		}
	}	
}

int main(){
	
	fscanf(f1,"%d%d%d",&n,&perm,&m);
	
	int x1,x2,i;
	
	for(i=1; i<=m; i++){
		fscanf(f1,"%d%d",&x1,&x2);
		a[x1][x2] = 1;
		a[x2][x1] = 1;
	}
	
	back(1);
	
	return 0;
}