Cod sursa(job #1605834)

Utilizator herbertoHerbert Mohanu herberto Data 19 februarie 2016 15:38:57
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <stdio.h>
#define MAXN 100001

using namespace std;

int sef[MAXN];
int find(int x){
  if(sef[x]==0)
    return x;
  sef[x]=find(sef[x]);
  return sef[x];
}

//int v[MAXN][3];
int main(){
  FILE*fin=fopen("disjoint.in", "r");
  FILE*fout=fopen("disjoint.out", "w");
  int n, k, p, a, b, i, x, y;
  fscanf(fin, "%d%d", &n, &k);
  for(i=1; i<=k; i++){
    fscanf(fin, "%d%d%d", &p, &a, &b);
    x=find(a);
    y=find(b);
    if(p==1)
      sef[x]=y;
    else{
      if(x==y)
        fprintf(fout, "DA\n");
      else
        fprintf(fout, "NU\n");
    }
  }
  return 0;
}