Pagini recente » Cod sursa (job #405554) | Cod sursa (job #1189730) | Cod sursa (job #2957843) | Borderou de evaluare (job #1036694) | Cod sursa (job #1806359)
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;
ifstream in ("disjoint.in");
ofstream out ("disjoint.out");
int tata[100010];
int h[100010];
int caut(int x)
{
int a;
if(x==tata[x])
return x;
else
{
a=caut(tata[x]);
tata[x]=a;
return a;
}
}
void unesc (int x,int y)
{
int a,b;
a=caut(x);
b=caut(y);
if(h[a]>h[b])
tata[a]=b;
else
{
tata[b]=a;
h[b]++;
}
}
int main()
{
srand(time(0));
int n,m,x,y,p,i;
in>>n>>m;
for(i=1;i<=n;i++)
{
tata[i]=i;
h[i]=1;
}
for(i=1;i<=m;i++)
{
in>>p>>x>>y;
if(p==1)
unesc(x,y);
else if(caut(x)==caut(y))
out<<"DA"<<endl;
else
out<<"NU"<<endl;
}
return 0;
}