Pagini recente » Cod sursa (job #2866694) | Cod sursa (job #1376978) | Cod sursa (job #1307045) | Cod sursa (job #1563251) | Cod sursa (job #1809819)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int t[100001],r[100001],i,j,k,n,m,caz,x1,y1;
int find1 (int x)
{
int rad=x;
//caut radacina
while(t[rad]!=0)
rad=t[rad];
int tmp;
//compresat caii (calea)
while(t[x]!=0)
{
tmp=t[x];
t[x]=rad;
x=tmp;
}
return rad;
}
void union1(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()
{
int x,y;
in>>n>>m;
for(i=1;i<=m;i++)
{
in>>caz>>x>>y;
if(caz==1)
{
x1=find1(x);
y1=find1(y);
union1(x1,y1);
}
else
{
if(find1(x)==find1(y))
out<<"DA"<<'\n';
else
out<<"NU"<<'\n';
}
}
return 0;
}