Pagini recente » Cod sursa (job #251679) | Cod sursa (job #1854984) | Cod sursa (job #512714) | Cod sursa (job #1510755) | Cod sursa (job #1437175)
#include <iostream>
#include <fstream>
using namespace std;
int findset(int *t,int *h,int x)
{
if (t[x]==0) return x;
t[x]=findset(t,h,t[x]);
return t[x];
}
void uniune(int *t,int *h,int x,int y)
{
if (h[x]<h[y]) t[findset(t,h,x)]=findset(t,h,y);
else t[findset(t,h,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;
}