Cod sursa(job #755529)

Utilizator visanrVisan Radu visanr Data 6 iunie 2012 00:18:22
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;

#define nmax 100010

int Father[nmax], Rank[nmax], N, M, x, y, c;

int Find(int x)
{
    if(Father[x] != x) x = Find(Father[x]);
    return Father[x];
}

void Unite(int x, int y)
{
     if(Rank[x] > Rank[y]) Father[y] = x;
     else Father[x] = y;
     if(Rank[x] == Rank[y]) Rank[y]++;
}

int main()
{
    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout);
    int i;
    scanf("%i %i", &N, &M);
    for(i = 1; i <= N; i++) Father[i] = i, Rank[i] = 1;
    for(i = 1; i <= M; i++)
    {
          scanf("%i %i %i", &c, &x, &y);
          if(c == 1) Unite(Find(x), Find(y));
          else if(Find(x) == Find(y)) printf("DA\n"); else printf("NU\n");
    }
    return 0;
}