Pagini recente » Cod sursa (job #627979) | Cod sursa (job #2394397) | Cod sursa (job #816302) | Cod sursa (job #2353934) | Cod sursa (job #1437170)
#include <iostream>
#include <fstream>
using namespace std;
int findset(int *t,int *h,int x)
{
int i=x;
while (t[x]!=0) x=t[x];
if (i!=x)
{
t[i]=x;
h[i]=1;
}
return x;
}
void uniune(int *t,int *h,int x,int y)
{
if (h[x]<h[y])
{
t[x]=findset(t,h,y);
h[x]++;
}
else
{
t[y]=findset(t,h,x);
h[y]++;
}
}
int main()
{
ifstream f("disjoint.in");
int a,b,c,*t,n,m,*h;
f>>n>>m;
t=new int [n+1];
h=new int [n+1];
for (int i=1;i<=n;i++) h[i]=t[i]=0;
ofstream g("disjoint.out");
for (int i=0;i<m;i++)
{
f>>a>>b>>c;
//for (int i=1;i<=n;i++) cout<<findset(t,i)<<" ";
//cout<<endl;
if (a==1) uniune(t,h,b,c);
else if (a==2)
{
if (findset(t,h,b)==findset(t,h,c)) g<<"DA\n";
else g<<"NU\n";
}
}
return 0;
}