Cod sursa(job #727802)

Utilizator veleanduAlex Velea veleandu Data 28 martie 2012 12:00:33
Problema Party Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.71 kb
#include <iostream>
#include  <fstream>
#include  <stdio.h>
using namespace std;
	
	typedef struct { long a,b; } pp;
	long n,m,i,a,b,c,bad,ok,s;
	pp R[1005];
	long V[105];

int main()
{
	// x y 0 -> x sau y trebe' sa participe
		// x | y
	// x y 1 -> daca x participa nu exista restrictie pentru y, dar daca x nu participa, nu exista restrictie pt y
		/*
			1 ? 0 => 1
			1 ? 1 => 1
			0 ? 0 => 1
			0 ? 1 => 0
			(X | !Y )
		*/
	// x y 2 -> daca y participa nu exista restrictie pentu x, ....
		/*
			(!x | y )
		*/
	// x y 3 -> cel putin unul dintre ei nu participa la petrecere
		/*
			!( x & y )
			sau
			((!x) | (!y))
		*/
	freopen("party.in","r",stdin);
	freopen("party.out","w",stdout);
	scanf("%d",&n);
	scanf("%d",&m);
	for ( i=1; i<=m; i++ )
	{
		scanf("%d %d %d",&a,&b,&c);
		if ( c==0 )
		{
			R[i].a=a;
			R[i].b=b;
		}
		if ( c==1 )
		{
			R[i].a=a;
			R[i].b=-b;
		}
		if ( c==2 )
		{
			R[i].a=-a;
			R[i].b=b;
		}
		if ( c==3 )
		{
			R[i].a=-a;
			R[i].b=-b;
		}
	}
	srand ( time(NULL) );
	for ( i=1; i<=n; i++ )
	{
		a=rand();
		V[i]=a%2;
	}
	while(1)
	{
		ok=1;
		// verificam daca e ok=ok
		for ( i=1; i<=m; i++ )
		{
			if ( R[i].a < 0 )
				a=V[R[i].a]+1;
			else
				a=V[R[i].a];
			if ( R[i].b < 0 )
				b=V[R[i].b]+1;
			else
				b=V[R[i].b];
			a%=2;
			b%=2;
			if ( a || b )
				;
			else{
				bad=i;
				ok=0;
			}		
		}
		if ( ok )
		{
			for ( i=1; i<=n; i++ )
				s+=V[i];
			printf("%d\n",s);
			for ( i=1; i<=n; i++ )
				if( V[i] )
					printf("%d\n",i);
			return 0;
		}		
		else{
			a=rand()%2;
			if ( a==0 )
				V[R[bad].a]++,V[R[bad].a]%=2;
			else
				V[R[bad].b]++,V[R[bad].b]%=2;
		}
	}
	return 0;
}