Cod sursa(job #1874410)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 9 februarie 2017 23:07:47
Problema Andrei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <vector>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <set>
#include <stack>

const int kMaxDim = 100005;

int nodeCount, edgeCount;
std::vector< int > edges[2*kMaxDim];
int level[2*kMaxDim], low[2*kMaxDim], currLevel;
std::stack< int > stack;
int value[2*kMaxDim];

inline int Not(int node) {
	return (node > nodeCount ? node - nodeCount : node + nodeCount);
}

inline void AddOrEdge(int first, int second) {
	edges[Not(first)].push_back(second);
	edges[Not(second)].push_back(first);
}

void Dfs(int node) {
	stack.push(node);
	level[node] = low[node] = ++currLevel;

	for (int adj : edges[node]) {
		if (!level[adj])
			Dfs(adj);
		if (level[adj] > 0)
			low[node] = std::min(low[node], low[adj]);
	}

	if (low[node] == level[node]) {
		int curr;
		do {
			curr = stack.top();
			stack.pop();

			level[curr] *= -1;

			if (value[curr] == 0) {
				value[curr] = 1;
				value[Not(curr)] = -1;
			}
		} while (curr != node);
	}
}

int main() {
	std::ifstream inputFile("andrei.in");
	std::ofstream outputFile("andrei.out");

	inputFile >> nodeCount >> edgeCount;
	for (int i = 1; i <= edgeCount; ++i) {
		int first, second, color;
		inputFile >> first >> second >> color;

		switch (color) {
			case 0:
				AddOrEdge(first, second);
				break;
			case 1:
				AddOrEdge(Not(first), Not(second));
				break;
			case 2:
				AddOrEdge(Not(first), second);
				AddOrEdge(first, Not(second));
				break;
		}
	}

	for (int i = 1; i <= 2*nodeCount; ++i)
		if (!level[i])
			Dfs(i);

	for (int i = 1; i <= nodeCount; ++i)
		outputFile << (value[i] == 1 ? 0 : 1) << ' ';
	outputFile << '\n';

	inputFile.close();
	outputFile.close();

	return 0;
}

//Trust me, I'm the Doctor!