Cod sursa(job #1654311)

Utilizator hrazvanHarsan Razvan hrazvan Data 16 martie 2016 22:33:05
Problema Andrei Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <stdio.h>
#include <string.h>
#define MAXN 100000
#define MAXM 200000
int n, nd1[4 * MAXM], nxt1[4 * MAXM], ut1[2 * MAXN], nd2[4 * MAXM], nxt2[4 * MAXM], ut2[2 * MAXN], dr;
char viz[2 * MAXN], val[2 * MAXN];
int vn[2 * MAXN], dv;

inline int nod(int x){
  if(x < 0)
    return -x - 1 + n;
  return x - 1;
}

inline int anod(int x){
  if(x >= n)
    return -(x - n + 1);
  return x + 1;
}

inline void add(int x, int y){
  nd1[dr] = y;
  nxt1[dr] = ut1[x];
  ut1[x] = dr;
  nd2[dr] = x;
  nxt2[dr] = ut2[y];
  ut2[y] = dr;
  dr++;
}

void topo(int x){
  viz[x] = 1;
  int poz = ut1[x];
  while(poz != -1){
    if(!viz[nd1[poz]])
      topo(nd1[poz]);
    poz = nxt1[poz];
  }
  vn[dv] = x;
  dv++;
}

void atr(int x){
  viz[x] = 1;
  viz[nod(-anod(x))] = 1;
  val[x] = 1;
  int poz = ut2[x];
  while(poz != -1){
    if(!viz[nd2[poz]])
      atr(nd2[poz]);
    poz = nxt2[poz];
  }
}

int main(){
  FILE *in = fopen("andrei.in", "r");
  int m, i, x, y, t;
  fscanf(in, "%d%d", &n, &m);
  memset(ut1, -1, sizeof ut1);
  memset(ut2, -1, sizeof ut2);
  for(i = 0; i < m; i++){
    fscanf(in, "%d%d%d", &x, &y, &t);
    if(t == 0){
      add(nod(-x), nod(y));
      add(nod(-y), nod(x));
    }
    else  if(t == 1){
      add(nod(x), nod(-y));
      add(nod(y), nod(-x));
    }
    else{
      add(nod(x), nod(y));
      add(nod(y), nod(x));
      add(nod(-x), nod(-y));
      add(nod(-y), nod(-x));
    }
  }
  fclose(in);
  for(i = 0; i < 2 * n; i++)
    if(!viz[i])
      topo(i);
  memset(viz, 0, sizeof viz);
  for(i = 2 * n - 1; i >= 0; i--){
    if(!viz[vn[i]])
      atr(vn[i]);
  }
  FILE *out = fopen("andrei.out", "w");
  for(i = 0; i < n; i++)
    fprintf(out, "%d ", !val[i]);
  fclose(out);
  return 0;
}