Cod sursa(job #1872686)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 8 februarie 2017 14:52:59
Problema Andrei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.76 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;
}

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--;
		if (type == 0) {
			// x V y
			G[2 * x + 1].push_back(2 * y);
			G[2 * y + 1].push_back(2 * x);
		} else if (type == 1) {
			// ~x V ~y
			G[2 * x].push_back(2 * y + 1);
			G[2 * y].push_back(2 * x + 1);
		} else {
			// (~x V y) & (x V ~y)
			G[2 * x].push_back(2 * y);
			G[2 * y].push_back(2 * x + 1);

			G[2 * y].push_back(2 * x);
			G[2 * x].push_back(2 * 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] << ' ';
	}
	cout << '\n';
	return 0;
}