Cod sursa(job #1653930)

Utilizator hrazvanHarsan Razvan hrazvan Data 16 martie 2016 18:16:42
Problema Party Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 1.95 kb
#include <stdio.h>
#define MAXN 100
#define MAXM 1000
int nd1[2 * MAXM], nxt1[2 * MAXM], ut1[2 * MAXN], nd2[2 * MAXM], nxt2[2 * MAXM], ut2[2 * MAXN];
int dr;
char viz[2 * MAXN], val[2 * MAXN];
int vn[2 * MAXN], dv;
int n;

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("party.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);
    switch(t){
      case 0:
        add(nod(-x), nod(y));
        add(nod(-y), nod(x));
        break;
      case 1:
        add(nod(-x), nod(-y));
        add(nod(y), nod(x));
        break;
      case 2:
        add(nod(-y), nod(-x));
        add(nod(x), nod(y));
        break;
      case 3:
        add(nod(x), nod(-y));
        add(nod(y), nod(-x));
        break;
    }
  }
  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("party.out", "w");
  int nr = 0;
  for(i = 0; i < n; i++)
    nr += !val[i];
  fprintf(out, "%d\n", nr);
  for(i = 0; i < n; i++)
    if(!val[i])
      fprintf(out, "%d\n", i + 1);
  fclose(out);
  return 0;;
}