Cod sursa(job #1872700)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 8 februarie 2017 15:18:32
Problema Andrei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <cstring>
#include <fstream>
#include <vector>
#define SZ(x) ((int) (x).size())
using namespace std;

const int NMAX = 100000;
const int NIL = -1;

vector<int> G[2 * NMAX];
int idx[2 * NMAX], stk[2 * NMAX];
bool onStack[2 * NMAX];

vector<int> component[2 * NMAX];
int numComponents;
bool value[2 * NMAX];

int tarjan(const int& node) {
	static int idxPtr, ss;
	int minPtr = idx[node] = idxPtr++;
	
	stk[ss++] = node;
	onStack[node] = true;
	for (const int& adj: G[node]) {
		if (idx[adj] == NIL) {
			minPtr = min(minPtr, tarjan(adj));
		} else if (onStack[adj]) {
			minPtr = min(minPtr, idx[adj]);
		}
	}

	if (minPtr == idx[node]) {
		int aux;
		do {
			aux = stk[--ss];
			onStack[aux] = false;
			component[numComponents].push_back(aux);
		} while (aux != node);
		numComponents++;
	}
	return minPtr;
}

void Or(const int& x, const int& y) {
	G[x ^ 1].push_back(y);
	G[y ^ 1].push_back(x);
}

int main() {
	ifstream cin("andrei.in");
	ofstream cout("andrei.out");
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	int n, m; cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int x, y, type; cin >> x >> y >> type; x--; y--;
		x <<= 1; y <<= 1;
		if (type == 0) {
			Or(x, y);
		} else if (type == 1) {
			Or(x ^ 1, y ^ 1);
		} else {
			Or(x ^ 1, y);
			Or(x, y ^ 1);
		}
	}

	memset(idx, NIL, sizeof idx);
	for (int i = 0; i < 2 * n; i++) {
		if (idx[i] == NIL) {
			tarjan(i);
		}
	}

	for (int i = numComponents - 1; i >= 0; i--) {
		if (not value[component[i].front()]) {
			for (const int& node: component[i]) {
				value[node ^ 1] = true;
			}
		}
	}

	for (int i = 0; i < n; i++) {
		cout << value[2 * i] << " \n"[i == n - 1];
	}
}