Cod sursa(job #468884)

Utilizator ilincaSorescu Ilinca ilinca Data 5 iulie 2010 13:04:04
Problema Andrei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <cstdio>
#include <vector>
#include <queue>

#define pmax 200005
#define pb push_back

using namespace std;

typedef vector <int> VI;
int n, nc, val [pmax];
VI c, g [pmax], gt [pmax];
bool viz [pmax];

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

void scan ()
{
	int m, a, b, s;
	scanf ("%d%d", &n, &m);
	for (; m; --m)
	{
		scanf ("%d%d%d", &a, &b, &s);
		if (s == 0)
		{
			g [neg (a)].pb (b);
			g [neg (b)].pb (a);
			gt [b].pb (neg (a));
			gt [a].pb (neg (b));
		}
		if (s == 1)
		{
			g [a].pb (neg (b));
			g [b].pb (neg (a));
			gt [neg (b)].pb (a);
			gt [neg (a)].pb (b);
		}
		if (s == 2)
		{
			g [a].pb (b);
			g [neg (a)].pb (neg (b));
			gt [b].pb (a);
			gt [neg (b)].pb (neg (a));
			g [b].pb (a);
			g [neg (b)].pb (neg (a));
			gt [a].pb (b);
			gt [neg (a)].pb (neg (b));
		}
	}
}

void dfs1 (int x)
{
	viz [x]=true;
	for (int i=0; i < g [x].size (); ++i)
		if (!viz [g [x] [i]]) dfs1 (g [x] [i]);
	c.pb (x);
}

void dfs2 (int x)
{
	viz [x]=true;
	viz [neg (x)]=true;
	val [x]=0;
	val [neg (x)]=1;
	for (int i=0; i < gt [x].size (); ++i)
		if (!viz [gt [x] [i]]) dfs2 (gt [x] [i]);
}

void scc ()
{
	int i;
	for (i=1; i <= 2*n; ++i)
		if (!viz [i]) dfs1 (i);
	memset (viz, 0, sizeof (viz));
	for (i=c.size ()-1; i >= 0; --i)
		if (!viz [c [i]]) dfs2 (c [i]);
}

void print ()
{
	for (int i=1; i <= n; ++i) printf ("%d ", val [grup [i]]);
	printf ("\n");
}

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