Cod sursa(job #1401313)

Utilizator cella.florescuCella Florescu cella.florescu Data 25 martie 2015 19:42:30
Problema Paduri de multimi disjuncte Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 0.73 kb
#include <stdio.h>
#include <stdlib.h>
int sef[100001];

inline int myfind(int pos){
  int stiva[100001], st;
  st=0;
  while(sef[pos]){
    stiva[st++]=pos;
    pos=sef[pos];
  }
  while(st>0)
    sef[stiva[--st]]=pos;
  return pos;
}

inline void myunion(int x, int y){
  sef[myfind(y)]=myfind(x);
}

int main()
{
    FILE *fin, *fout;
    int i, n, m, op, x, y;
    fin=fopen("disjoint.in", "r");
    fscanf(fin, "%d%d", &n, &m);
    fout=fopen("disjoint.out", "w");
    for(i=0; i<m; i++){
      fscanf(fin, "%d%d%d", &op, &x, &y);
      if(op==1)
        myunion(x, y);
      else if(myfind(x)==myfind(y))
          fprintf(fout, "DA\n");
        else
          fprintf(fout, "NU\n");
    }
    fclose(fin);
    fclose(fout);
    return 0;
}