Pagini recente » Cod sursa (job #2845154) | Cod sursa (job #2721299) | Cod sursa (job #1436916) | Cod sursa (job #52191) | Cod sursa (job #2149908)
#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;
xRoot=Find(x);
yRoot=Find(y);
if(R[xRoot]<=R[yRoot])
{
T[xRoot]=yRoot;
R[yRoot]+=R[xRoot];
}
else
{
T[yRoot]=xRoot;
R[xRoot]+=R[yRoot];
}
}
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;
}