Pagini recente » Cod sursa (job #1592925) | Cod sursa (job #2654543) | Cod sursa (job #1824966) | Cod sursa (job #313314) | Cod sursa (job #1448488)
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
#define MAX 100014
int tata[MAX];
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int str(int nod);
void unire(int nod1, int nod2);
int main()
{
int n,m,x,y,z;
fin>>n>>m;
for(int i=1;i<=n;i++)
{
tata[i]=i;
//rang[i]=1;
}
for(int i=1; i<=m; ++i)
{
fin>>z>>x>>y;
if(z==1)
{
x=str(x);
y=str(y);
unire(x,y);
}
else
{
if(str(y)==str(x))
fout<<"DA"<<'\n';
else
fout<<"NU"<<'\n';
}
}
return 0;
}
int str(int nod)
{
int x=nod;
while(nod!=tata[nod])
nod=tata[nod];
while(x!=tata[x])
{
x=tata[x];
tata[x]=nod;
}
return nod;
}
void unire(int nod1, int nod2)
{
srand (time(NULL));
int x=rand()%2;
if(x==0)
tata[nod2]=nod1;
else
tata[nod1]=nod2;
}