Pagini recente » Cod sursa (job #2320046) | Cod sursa (job #2068497) | Cod sursa (job #740329) | Cod sursa (job #2334636) | Cod sursa (job #2682083)
#include<iostream>
#include<fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int h[100005],t[100005],n,m,C,x,y,aux,rad;
int find(int k)
{
int rad=k;
while(t[rad])
rad=t[rad];
while(t[x])
{
aux=t[x];
t[x]=rad;
x=aux;
}
return rad;
}
void uniune(int a, int b)
{
if(h[a]>h[b])
t[b]=a;
else
if(h[a]<h[b])
t[a]=b;
else
{
t[b]=a;
h[a]++;
}
}
int main()
{
fin>>n>>m;
while(m--)
{
fin>>C;
fin>>x>>y;
if(C==1)
{
x=find(x);
y=find(y);
uniune(x,y);
}
if(C==2)
if(find(x)==find(y))
fout<<"DA\n";
else
fout<<"NU\n";
}
fin.close();
fout.close();
return 0;
}