Pagini recente » Cod sursa (job #481815) | Cod sursa (job #1233835) | Cod sursa (job #393568) | Cod sursa (job #624985) | Cod sursa (job #2525960)
#include <bits/stdc++.h>
#define NMAX 100009
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int tata[NMAX], h[NMAX]; ///inalt arborelui cu rad i
int n;
//fct
int Find(int x); ///return la radacina arborelui caruia apartine x
void Union(int x, int y); ///reuneste cu arborele cu rad x si cel cu raad y
int m,i;
int main()
{fin>>n>>m;
int t,i,op,x,y;
fin>>n>>m;
for(i=1;i<=m;i++){
fin>>op>>x>>y;
if(op & 1){
if(Find(x) != Find(y))
Union(x,y);
}
else{
if(Find(x) == Find(y))
fout<<"DA\n";
else
fout<<"NU\n";
}
}
return 0;
}
int Find(int x)
{
int rad,aux;
rad=x;
while(tata[rad]) rad=tata[rad];
///fac compresia drumului
while(tata[x])
{aux=tata[x];
tata[x]=rad;
x=aux;
}
return rad;
///O(n)
}
void Union(int x, int y)
{
tata[y]=x; ///O(1)
if(h[x]<h[y])
tata[x]=y;
else
{tata[y]=x;
if(h[x]==h[y])
h[x]++;
}
}