Pagini recente » Cod sursa (job #1460694) | Cod sursa (job #112794) | Cod sursa (job #1319565) | Cod sursa (job #1110756) | Cod sursa (job #1499474)
#include <iostream>
#include <fstream>
#define nmax 100007
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
long tata[nmax],h[nmax];
void uneste(long x,long y)
{if(h[x]>h[y])tata[y]=x;
else
{tata[x]=y;
if(h[x]==h[y])h[y]++;
}
}
long cauta(long x)
{long r,aux,y;
r=x;
while(tata[r])r=tata[r];
y=x;
while(y!=r)
{aux=tata[y];
tata[y]=r;
y=aux;}
return r;
}
int main()
{long n,m,i,op,x,y;
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>op>>x>>y;
if(op==1)uneste(cauta(x),cauta(y));
if(op==2)
if(cauta(x)==cauta(y))
fout<<"DA\n";
else fout<<"NU\n";
}
}