Pagini recente » Cod sursa (job #1993562) | Cod sursa (job #450783) | Cod sursa (job #1549338) | Cod sursa (job #2360772) | Cod sursa (job #1437173)
#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;
return x;
}
void uniune(int *t,int *h,int x,int y)
{
if (h[x]<h[y])
t[x]=findset(t,h,y);
else
t[y]=findset(t,h,x);
if (h[x]==h[y]) h[x]++;
}
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;
}