Cod sursa(job #2295997)

Utilizator stan_flaviusStan Flavius Stefan stan_flavius Data 4 decembrie 2018 02:43:17
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>
#define nmax 100001
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

int n,a[nmax];
int m;
int t[nmax],h[nmax];

int Find(int i)
{ if(t[i]==-1) return i;
  else Find(t[i]);
}

void Union(int x,int y)
{ int Mx=Find(x);
  int My=Find(y);
  if(Mx!=My)
     if(h[Mx]>h[My])t[Mx]=My;
     else t[My]=Mx;
}

int main()
{ fin>>n>>m;
  int i,q,x,y;
  for(i=1; i<=n; i++) {t[i]=-1; h[i]=1;}
  for(i=1; i<=m; i++)
      { fin>>q>>x>>y;
        if(q==1) Union(x,y);
        if(q==2)
           { if(Find(x)==Find(y)) fout<<"DA";
             else fout<<"NU";
             fout<<"\n";
           }
      }
    return 0;
}