Pagini recente » Cod sursa (job #1400255) | Cod sursa (job #1653910) | Cod sursa (job #949089) | Cod sursa (job #243716) | Cod sursa (job #1809820)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int t[100001],q,r[100001],n,m;
int caut( int x)
{
int rad = x;
while(t[rad] != 0)
rad=t[rad];
int tmp;
while(t[x]!=0)
{
tmp=t[x];
t[x]=rad;
x=tmp;
}
return rad;
}
void unire(int x, int y)
{
if(r[x] > r[y])
t[y]=x;
else
if(r[x] < r[y])
t[x]=y;
else
{
t[y]=x;
r[x]++;
}
}
int main()
{
in >> n >> m;
int x,y;
for(int i= 1 ; i <= m; i++)
{
in>>q>>x>>y;
if(q == 1)
{
unire(caut(x),caut(y));
}
else
if(caut(x) == caut (y))
out<<"DA" << '\n';
else
out<<"NU" << '\n';
}
return 0;
}