Cod sursa(job #467348)

Utilizator AndreyPAndrei Poenaru AndreyP Data 28 iunie 2010 14:29:06
Problema Andrei Scor 10
Compilator cpp Status done
Runda Stelele Informaticii 2010, clasele X-XII, Ziua 2 Marime 2.7 kb
#include <cstdio>
#include <cstdlib>
#include <bitset>
#include <vector>
#include <queue>
using namespace std;
#define N 100010
#define pb push_back

int n;
char cul[N];
vector< int > a[N][3];
char unde[N];
bitset< N > viz,gata;
bitset< 35 > e[3][35];
queue< int > q;
int v[N];
char rez[N];

void scrie(int conf)
{
	--n;
	for(int i=0; i<n; ++i)
	{
		if((conf&(1<<i))!=0)
			printf("1 ");
		else
			printf("0 ");
	}
	if((conf&(1<<n))!=0)
		printf("1\n");
	else
		printf("0\n");
	exit(0);
}

void back(int nod,int conf)
{
	if(nod==n)
		scrie(conf);
	bool ok=true;
	for(int i=0; i<nod && ok; ++i)
	{
		if((conf&(1<<i))!=0)
		{
			if(e[2][nod][i]==1)
				ok=false;
		}
		else
		{
			if(e[0][nod][i]==1)
				ok=false;
		}
	}	
	if(ok && e[0][nod][nod]==1)
		ok=false;
	if(ok)
		back(nod+1,conf);
	
	ok=true;
	for(int i=0; i<nod && ok; ++i)
	{
		if((conf&(1<<i))!=0)
		{
			if(e[1][nod][i]==1)
				ok=false;
		}
		else
		{
			if(e[2][nod][i]==1)
				ok=false;
		}
	}
	if(ok && e[1][nod][nod]==1)
		ok=false;
	if(ok)
		back(nod+1,(conf|(1<<nod)));
}

inline void citire()
{
	int m;
	scanf("%d%d",&n,&m);
	int x,y,z;
	if(n<32)
	{
		for(int i=0; i<m; ++i)
		{
			scanf("%d%d%d",&x,&y,&z);
			--x;
			--y;
			e[z][x][y]=1;
			e[z][y][x]=1;
		}
		back(0,0);
		//exit(0);
	}
	
	for(int i=0; i<m; ++i)
	{
		scanf("%d%d%d",&x,&y,&z);
		a[x][z].pb(y);
		a[y][z].pb(x);
	}
}

inline int bfs(int now)
{
	viz.reset();
	viz[now]=1;
	q.push(now);
	v[0]=1;
	v[1]=now;
	
	while(!q.empty())
	{
		now=q.front();
		q.pop();
		for(int t=0; t<3; ++t)
		{
			for(size_t i=0,lim=a[now][t].size(); i<lim; ++i)
			{
				if(viz[a[now][t][i]]==1)
					continue;
				viz[a[now][t][i]]=0;
				v[++v[0]]=a[now][t][i];
				q.push(a[now][t][i]);
			}
		}
	}
	
	int nod;
	for(int j=1; j<=v[0]; ++j)
	{
		nod=v[j];
		gata[nod]=1;
		for(int t=0; t<3; ++t)
		{
			for(size_t i=0,lim=a[nod][t].size(); i<lim; ++i)
			{
				if(viz[a[nod][t][i]]==0)
					continue;
				return t;
			}
		}
	}
	
	return 5;
}

inline void pune(int x)
{
	char y=(char)x;
	for(int i=1; i<=v[0]; ++i)
		rez[v[i]]=y;
}

inline void rezolva()
{
	srand(time(NULL));
	int aux;
	for(int i=1; i<=n; ++i)
	{
		if(gata[i]==1)
			continue;
		aux=bfs(i);
		if(aux==5)
			pune(rand()&1);
		else
		{
			if(aux==0)
				pune(1);
			else
				pune(0);
		}
	}
	
	for(int i=1; i<n; ++i)
	{
		if(rez[i]==0)
			fputs("0 ",stdout);
		else
			fputs("1 ",stdout);
	}
	
	if(rez[n]==0)
		fputs("0\n",stdout);
	else
		fputs("1\n",stdout);
}

int main()
{
	freopen("andrei.in","r",stdin);
	freopen("andrei.out","w",stdout);
	
	citire();
	rezolva();
	
	return 0;
}