Cod sursa(job #2608072)

Utilizator Mihai145Oprea Mihai Adrian Mihai145 Data 30 aprilie 2020 15:42:19
Problema Andrei Scor 100
Compilator cpp-32 Status done
Runda Arhiva de probleme Marime 2.54 kb
#include <fstream>
#include <vector>
#include <stack>
 
using namespace std;
 
ofstream cout("andrei.out");

	
class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;
 
	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}
 
public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}
	
	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
	
	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};

const int NMAX = 2e5;
 
int N, M;
vector <int> g[NMAX + 5];
vector <int> t[NMAX + 5];
 
bool seen[NMAX + 5];
stack <int> st;
void dfs1(int node)
{
	seen[node] = true;
 
	for(auto it : g[node])
		if(!seen[it]) dfs1(it);
 
	st.push(node);
}
 
int kComp, comp[NMAX + 5];
void dfs2(int node)
{
	comp[node] = kComp;
 
	for(auto it : t[node])
		if(comp[it] == 0) dfs2(it);
}
 
int Nrm(int x){return x;}
int Not(int x){return x + N;}
 
int main()
{
	InParser cin("andrei.in");
	
	cin >> N >> M;
 
	for(int i = 1; i <= M; i++)
		{
			int x, y, z;
			cin >> x >> y >> z;
 
			if(z == 1)
				{
					g[Not(x)].push_back(Nrm(y));
					g[Not(y)].push_back(Nrm(x));
 
					t[Nrm(y)].push_back(Not(x));
					t[Nrm(x)].push_back(Not(y));
				}
			else if(z == 0)
				{
					g[Nrm(x)].push_back(Not(y));
					g[Nrm(y)].push_back(Not(x));
 
					t[Not(y)].push_back(Nrm(x));
					t[Not(x)].push_back(Nrm(y));
				}	
			else
				{
					g[Nrm(x)].push_back(Nrm(y));
					g[Not(x)].push_back(Not(y));
 
					t[Nrm(y)].push_back(Nrm(x));
					t[Not(y)].push_back(Not(x));
 
					g[Nrm(y)].push_back(Nrm(x));
					g[Not(y)].push_back(Not(x));
 
					t[Nrm(x)].push_back(Nrm(y));
					t[Not(x)].push_back(Not(y));
				}
		}
 
	for(int i = 1; i <= 2 * N; i++)
		if(!seen[i]) dfs1(i);
 
	while(!st.empty())
	{
		int node = st.top(); st.pop();
 
		if(comp[node] == 0)
		{
			++kComp;
			dfs2(node);
		}
	}
 
	for(int i = 1; i <= N; i++)
		cout << (comp[i + N] > comp[i]) << ' ';
 
	return 0;
}