Cod sursa(job #800943)

Utilizator ChallengeMurtaza Alexandru Challenge Data 22 octombrie 2012 22:25:44
Problema Andrei Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.18 kb
/*
	PROB: andrei
	LANG: C++
*/

#include <fstream>
#include <iostream>
#include <vector>

#define DEBUG

#ifndef DEBUG
	#define PRINT(x)
	#define D if(0)
#else
	#define PRINT(x) \
		cout<<#x<<":\t"<<x<<endl
	#define D if(1)
#endif

using namespace std;

const char InFile[]="andrei.in";
const char OutFile[]="andrei.out";
const int MaxN=200111;

ifstream fin(InFile);
ofstream fout(OutFile);

int N,M,x,y,op,comp,C[MaxN],S[MaxN],ind,low[MaxN],niv[MaxN];
char SOL[MaxN];
vector<int> G[MaxN],GC[MaxN],El[MaxN];

inline int NON(const int &x)
{
	if(x>N)
	{
		return x-N;
	}
	return x+N;
}

void Tarjan(int nod)
{
	niv[nod]=low[nod]=++ind;
	S[++S[0]]=nod;
	for(vector<int>::iterator it=G[nod].begin();it!=G[nod].end();++it)
	{
		int &vcn=*it;
		if(!low[vcn])
		{
			Tarjan(vcn);
		}
		else if(low[vcn]>0)
		{
			if(low[vcn]<low[nod])
			{
				low[nod]=low[vcn];
			}
		}
	}

	if(niv[nod]==low[nod])
	{
		++comp;
		int tnod;
		do
		{	
			tnod=S[S[0]];
			--S[0];
			C[tnod]=comp;
			low[tnod]=-low[tnod];
			El[comp].push_back(tnod);
		}while(tnod!=nod);
	}
}

void DFS(int nod)
{
	low[nod]=0;
	for(vector<int>::iterator it=GC[nod].begin();it!=GC[nod].end();++it)
	{
		if(low[*it])
		{
			DFS(*it);
		}
	}

	S[++S[0]]=nod;
}

int main()
{
	fin>>N>>M;
	for(register int i=1;i<=M;++i)
	{
		fin>>x>>y>>op;
		if(op==0)
		{
			G[NON(x)].push_back(y);
			G[NON(y)].push_back(x);
		}
		else if(op==1)
		{
			G[x].push_back(NON(y));
			G[y].push_back(NON(x));
		}
		else
		{
			G[x].push_back(y);
			G[y].push_back(x);

			G[NON(x)].push_back(NON(y));
			G[NON(y)].push_back(NON(x));
		}
	}
	fin.close();
	
	for(register int i=1;i<=(N<<1);++i)
	{
		if(!low[i])
		{
			Tarjan(i);
		}
	}

	for(register int i=1;i<=(N<<1);++i)
	{
		for(vector<int>::iterator it=G[i].begin();it!=G[i].end();++it)
		{
			GC[C[i]].push_back(C[*it]);
		}
	}

	for(register int i=1;i<=(N<<1);++i)
	{
		if(low[i])
		{
			DFS(i);
		}
	}

	while(S[0])
	{
		int c=S[S[0]];
		for(vector<int>::iterator it=El[c].begin();it!=El[c].end();++it)
		{
			int &x=*it;
			if(!SOL[x])
			{
				SOL[x]=-1;
				SOL[NON(x)]=1;
			}
		}
		--S[0];
	}

	for(register int i=1;i<=N;++i)
	{
		if(SOL[i]==-1)
		{
			fout<<"0 ";
		}
		else
		{
			fout<<"1 ";
		}
	}
	fout.close();
	return 0;
}