Cod sursa(job #2049621)

Utilizator ApostolIlieDanielApostol Daniel ApostolIlieDaniel Data 27 octombrie 2017 15:04:23
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 100000;

int tata[MAXN + 1];

int sef (int x) {
  if (tata[x] == x)
    return x;
  else
    return tata[x] = sef(tata[x]);
}

void unire (int x, int y) {
  int tx, ty;
  tx = sef (x);
  ty = sef (y);
  tata[tx] = ty;
}


int main()
{
  FILE *fin, *fout;
  int n, m, i, c, x, y, tx, ty;
  fin = fopen ("disjoint.in", "r");
  fout = fopen ("disjoint.out", "w");
  fscanf (fin, "%d%d", &n, &m);
  for (i = 1; i <= n; i++)
    tata[i] = i;
  for (i = 1; i <= m; i++) {
    fscanf (fin, "%d%d%d", &c, &x, &y);
    if (c == 1) {
      unire (x, y);
    }
    else {
      if (sef (x) == sef (y))
        fprintf (fout, "DA\n");
      else
        fprintf (fout, "NU\n");
    }
  }
  fclose (fin);
  fclose (fout);
    return 0;
}