Cod sursa(job #569629)

Utilizator rootsroots1 roots Data 1 aprilie 2011 21:23:15
Problema Party Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define Dim1 101
#define Dim2  1001

struct vector
{
	int x,y,z;
}v[Dim2];

char sol[Dim1];

int main()
{
	int M,N,i,nr,end,pos;

	freopen("party.in","r",stdin);

	scanf("%d%d",&N,&M);
	for(i=1;i<=M;i++) scanf("%d%d%d",&v[i].x,&v[i].y,&v[i].z);

	srand(time(NULL));
	for(i=1;i<=N;i++) sol[i]=rand()%2;

	end=0;
	while(!end)
	{
		end=1;
		pos=rand()%M+1;

		for(i=pos;i<=M;i++)
			if(v[i].z==0)
			{
				if(sol[v[i].x]|sol[v[i].y]==0)
				{
					nr=rand()%2;
					if(!nr) sol[v[i].x]=1-sol[v[i].x];
					else sol[v[i].y]=1-sol[v[i].y];
					end=0;
					break;
				}
			}
			else
			if(v[i].z==1)
			{
				if(sol[v[i].x]==0&&sol[v[i].y]!=0)
				{
					nr=rand()%2;
					if(nr) sol[v[i].x]=1-sol[v[i].x];
					else sol[v[i].y]=1-sol[v[i].y];
					end=0;
					break;
				}
			}
			else
			if(v[i].z==2)
			{
				if(sol[v[i].y]==0&&sol[v[i].x]!=0)
				{
					nr=rand()%2;
					if(nr) sol[v[i].x]=1-sol[v[i].x];
					else sol[v[i].y]=1-sol[v[i].y];
					end=0;
					break;
				}
			}
			else
			if(v[i].z==3)
			{
				if(sol[v[i].x]&sol[v[i].y]==1)
				{
					nr=rand()%2;
					if(!nr) sol[v[i].x]=1-sol[v[i].x];
					else sol[v[i].y]=1-sol[v[i].y];
					end=0;
					break;
				}
			}
	}

	freopen("party.out","w",stdout);

	nr=0;
	for(i=1;i<=N;i++) nr+=sol[i];
	printf("%d\n",nr);
	for(i=1;i<=N;i++)
		if(sol[i]) printf("%d\n",i);

	return 0;
}