Cod sursa(job #469181)

Utilizator AndreyPAndrei Poenaru AndreyP Data 6 iulie 2010 19:20:37
Problema Andrei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <cstdio>
#include <vector>
#include <bitset>
using namespace std;
#define pb push_back
#define N 100010

int n;
vector< int > a[N<<1],at[N<<1];
bitset< N<<1 > viz;
char rez[N<<1];
int st[N<<1];

inline int neg(int x)
{
	if(x<=n)
		return x+n;
	return x-n;
}

inline void sau(int x,int y)
{
	a[neg(x)].pb(y);
	a[neg(y)].pb(x);
	at[y].pb(neg(x));
	at[x].pb(neg(y));
}

inline void citire()
{
	int m;
	scanf("%d%d",&n,&m);
	int x,y,c;
	for(int i=0; i<m; ++i)
	{
		scanf("%d%d%d",&x,&y,&c);
		if(c==0)
		{
			sau(x,y);
			continue;
		}
		if(c==1)
		{
			sau(neg(x),neg(y));
			continue;
		}
		sau(neg(x),y);
		sau(x,neg(y));
	}
}

void dfs(int nod)
{
	viz[nod]=1;
	for(vector< int >::iterator it=a[nod].begin(),lim=a[nod].end(); it!=lim; ++it)
	{
		if(viz[*it]==1)
			continue;
		dfs(*it);
	}
	st[++st[0]]=nod;
}

void dfst(int nod)
{
	viz[nod]=1;
	rez[nod]='0';
	rez[neg(nod)]='1';
	for(vector< int >::iterator it=at[nod].begin(),lim=at[nod].end(); it!=lim; ++it)
	{
		if(viz[*it]==1)
			continue;
		dfst(*it);
	}
}

inline void rezolva()
{
	int n1=n<<1;
	for(int i=1; i<=n1; ++i)
	{
		if(viz[i]==1)
			continue;
		dfs(i);
	}

	viz.reset();
	for(int i=st[0]; i>0; --i)
	{
		if(viz[st[i]]==1 || viz[neg(st[i])]==1)
			continue;
		dfst(st[i]);
	}
}

inline void scrie()
{
	for(int i=1; i<n; ++i)
		printf("%c ",rez[i]);
	printf("%c\n",rez[n]);
}

int main()
{
	freopen("andrei.in","r",stdin);
	freopen("andrei.out","w",stdout);

	citire();
	rezolva();
	scrie();

	return 0;
}