Pagini recente » Cod sursa (job #2794088) | Cod sursa (job #301666) | Cod sursa (job #3189888) | Cod sursa (job #2066113) | Cod sursa (job #2691812)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int tata[100001], h[100001];
int Reprezentant(int x)
{
int rez = x;
while(tata[rez]!=rez)
{
rez=tata[rez];
}
while(tata[x]!=x)
{
int aux = tata[x];
tata[x]= rez;
x=aux;
}
return rez;
}
void Reuniune(int x, int y)
{
if(h[x]>h[y])
tata[y]=x;
else
{
tata[x]=y;
if(h[x]==h[y])
h[y]++;
}
}
int main()
{
int n,m;
fin>>n>>m;
for(int i=1;i<=n;i++)
{
tata[i]=i;
h[i]=1;
}
while(m--)
{
int op,x,y;
fin>>op>>x>>y;
if(op==2)
{
if(Reprezentant(x)==Reprezentant(y))
fout<<"DA";
else fout<<"NU";
fout<<"\n";
}
else{
Reuniune(Reprezentant(x),Reprezentant(y));
}
}
return 0;
}