Cod sursa(job #2172148)

Utilizator GoogalAbabei Daniel Googal Data 15 martie 2018 15:17:07
Problema Andrei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <memory.h>
#include <iostream>
#include <fstream>
#include <bitset>
#include <vector>
#include <stack>

using namespace std;

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

const int NMAX = 2 * 1e5;

int n, m;
vector < int > g[1 + NMAX];
vector < int > gi[1 + NMAX];

bitset < 1 + NMAX > vis;
bitset < 1 + NMAX > val;

stack < int > st;

int nott(int x) {
  if(x > n)
    return x - n;
  else
    return x + n;
}

void addedge(int x, int y) {
  g[x].push_back(y);
  gi[y].push_back(x);
}

void dfs1(int node) {
  vis[node] = 1;
  for(int i : g[node])
    if(vis[i] == 0)
      dfs1(i);
  st.push(node);
}

void dfs2(int node) {
  vis[node] = 1;
  val[nott(node)] = 1;
  for(int i : gi[node])
    if(vis[i] == 0)
      dfs2(i);
}

void setup(int x, int y) {
  addedge(nott(x), y);
  addedge(nott(y), x);
}

int main()
{
  in >> n >> m;
  for(int i = 1; i <= m; i++) {
    int x, y, col;
    in >> x >> y >> col;
    if(col == 0) {
      setup(x, y);
    } else if(col == 1) {
      setup(nott(x), nott(y));
    } else {
      setup(x, nott(y));
      setup(nott(x), y);
    }
  }

  for(int i = 1; i <= 2 * n; i++)
    if(vis[i] == 0)
      dfs1(i);
  vis = 0;
  while(!st.empty()) {
    int from = st.top();
    st.pop();
    if(vis[from] == 0 && vis[nott(from)] == 0)
      dfs2(from);
  }

  for(int i = 1; i <= n; i++)
    out << val[i] << ' ';
  out << '\n';

  in.close();
  out.close();
  return 0;
}