Cod sursa(job #796747)

Utilizator radustn92Radu Stancu radustn92 Data 12 octombrie 2012 14:19:45
Problema 2SAT Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <algorithm>
#define NMAX 100005
#define LMAX 200005
#define pii pair <int,int>
#define x first
#define y second
using namespace std;
int n,m,steps;
pii A[LMAX];
bool val[LMAX],work=1;
inline int change(int x)
{
	return x<0 ? -x+n : x;
}
inline int sw(int x)
{
	return x<=n ? x+n : x-n;
}
int main()
{
	freopen("2sat.in","r",stdin);
	freopen("2sat.out","w",stdout);
	srand(time(0));
	scanf("%d%d",&n,&m);
	int i,a,b,edge;
	for (i=1; i<=m; i++)
	{
		scanf("%d%d",&a,&b);
		a=change(a); b=change(b);
		A[i].x=a; A[i].y=b;
	}
	for (i=1; i<=n; i++)
		val[i]=rand()%2,val[i+n]=val[i]^1;
	while (work && steps<=n*20)
	{
		work=0; steps++;
		for (i=1; i<=m; i++)
			if ( !val[A[i].x] && !val[A[i].y] )
			{
				edge=i; work=1;
				break ;
			}
			
		if (work)
		{
			if (rand()%2)
				val[A[edge].x]^=1,val[sw(A[edge].x)]^=1;
			else
				val[A[edge].y]^=1,val[sw(A[edge].y)]^=1;
		}
	}
	if (work)
	{
		printf("-1\n");
		return 0;
	}
	for (i=1; i<=n; i++)
		printf("%d ",val[i]);
	printf("\n");
	return 0;
}