Cod sursa(job #1869402)

Utilizator marcdariaDaria Marc marcdaria Data 5 februarie 2017 19:46:47
Problema Andrei Scor 100
Compilator cpp Status done
Runda becreative8 Marime 1.52 kb
#include <fstream>
#include <vector>
#include <stack>
#include <cstring>
using namespace std;

const int kMaxNodes = 200005;

ifstream fin("andrei.in");
ofstream fout("andrei.out");

int N;
vector<int> g[kMaxNodes];
vector<int> gt[kMaxNodes];

bool use[kMaxNodes];
stack<int> st;

bool val[kMaxNodes];

int Not(int x) {
  if (x > N) return x - N;
  return x + N;
}

void AddEdge(int x, int y) {
  g[x].push_back(y);
  gt[y].push_back(x);
}

void Or(int x, int y) {
  AddEdge(Not(x), y);
  AddEdge(Not(y), x);
}

void Init() {
  int M;
  fin >> N >> M;
  while (M--) {
    int x, y, c;
    fin >> x >> y >> c;
    switch (c) {
      case 0:
        Or(x, y);
        break;
      case 1:
        Or(Not(x), Not(y));
        break;
      default:
        Or(x, Not(y));
        Or(Not(x), y);
    }
  }
}

void SortDfs(int node) {
  use[node] = true;
  for (int i : g[node])
    if (!use[i])
      SortDfs(i);
  st.push(node);
}

void SolveDfs(int node) {
  use[node] = true;
  val[Not(node)] = true;
  for (int i : gt[node])
    if (!use[i])
      SolveDfs(i);
}

void Solve() {
  for (int i = 1; i <= 2 * N; ++i)
    if (!use[i])
      SortDfs(i);
  memset(use, 0, sizeof use);
  while (!st.empty()) {
    int node = st.top();
    st.pop();
    if (!use[node] && !use[Not(node)])
      SolveDfs(node);
  }
}

void Print() {
  for (int i = 1; i <= N; ++i)
    fout << int(val[i]) << " ";
  fout << "\n";
}

int main() {
  Init();
  Solve();
  Print();
  return 0;
}