Pagini recente » Cod sursa (job #2315047) | Cod sursa (job #338382) | Cod sursa (job #2335003) | Cod sursa (job #333580) | Cod sursa (job #2149907)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int N,M,cod,x,y,T[100005],R[100005];
void Make_set(int m)
{
for(int i=1;i<=m;i++)
{
T[i]=i;
R[i]=1;
}
}
int Find(int x)
{
if(x!=T[x]) T[x]=Find(T[x]);
return T[x];
}
void Union(int x, int y)
{
int xRoot, yRoot;
int a,b;
xRoot=Find(x);
yRoot=Find(y);
if(R[xRoot]<=R[yRoot])
T[xRoot]=yRoot;
else T[yRoot]=xRoot;
}
int main()
{
fin>>N>>M;
Make_set(M);
for(int i=1;i<=M;i++)
{
fin>>cod>>x>>y;
if(cod==1) Union(x,y);
else
{
if(Find(x)!=Find(y)) fout<<"NU"<<'\n';
else fout<<"DA"<<'\n';
}
}
return 0;
}