Pagini recente » Cod sursa (job #804074) | Cod sursa (job #320633) | Cod sursa (job #2727030) | Cod sursa (job #278753) | Cod sursa (job #3261367)
#include <fstream>
#define NMAX 100002
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int N,M,rad[NMAX],nr[NMAX];
int Find(int x)
{
if(rad[x]==x)
{
return x;
}
rad[x]=Find(rad[x]);
return rad[x];
}
void Union(int a, int b)
{
if(nr[a]>nr[b])
{
rad[b]=a;
nr[a]+=nr[b];
}
else
{
rad[a]=b;
nr[b]+=nr[a];
}
}
int main()
{
int tip,x,y;
fin>>N>>M;
for(int i=1; i<=N; i++)
{
rad[i]=i;
nr[i]=1;
}
for(int i=1; i<=M; i++)
{
fin>>tip>>x>>y;
int sef1,sef2;
sef1=Find(x);
sef2=Find(y);
if(tip==1)
{
Union(sef1,sef2);
}
else
{
if(sef1==sef2)
{
fout<< "DA";
}
else
{
fout<< "NU";
}
fout<< "\n";
}
}
return 0;
}