Cod sursa(job #1363317)

Utilizator jacobshelo there jacobs Data 26 februarie 2015 21:23:06
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <cstdio>
# define N 100010
using namespace std;
int r[N],t[N],x,y,n,m,i,op;
int tata(int x)
{
    if(t[x]!=x) t[x]=tata(t[x]);
    return t[x];
}
bool reuneste (int x, int y)
{
    if(r[x]>r[y]) t[y]=x;
    else t[x]=y;
    if(r[x]==r[y]) r[x]++;
}

int main()
{
    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);
    scanf("%d %d",&n,&m);
    for(i=1;i<=n;++i)
          t[i]=i,r[i]=1;
     for(i=1;i<=m;++i)
     {
         scanf("%d %d %d",&op,&x,&y);
         if(op==1)
           reuneste(tata(x),tata(y));
         else
             if (tata(x)==tata(y)) printf("DA\n");
                else printf("NU\n");
     }
    return 0;
}