Pagini recente » Cod sursa (job #3208736) | Cod sursa (job #483383) | Cod sursa (job #629780) | Cod sursa (job #1374258) | Cod sursa (job #1999507)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int p[100005],h[100005];
int FindSet(int x)
{
while(p[x]!=x)
x=p[x];
return x;
}
void UnionSet(int x,int y)
{
if(h[x]==h[y])
{
p[y]=x;
++h[x];
}
else if(h[x]>h[y])
{
p[y]=x;
++h[x];
}
else if(h[x]<h[y])
{
p[x]=y;
++h[y];
}
}
int main()
{
int n,m,i,o,ta,tb,a,b;
in>>n>>m;
for(i=1;i<=n;++i)
{
p[i]=i;h[i]=1;
}
for(i=1;i<=m;++i)
{
in>>o>>a>>b;
if(o==1)
{
ta=FindSet(a);
tb=FindSet(b);
if(ta!=tb)
UnionSet(ta,tb);
}
if(o==2)
{
if(FindSet(a)==FindSet(b))
out<<"DA"<<'\n';
else
out<<"NU"<<'\n';
}
}
return 0;
}