Pagini recente » Cod sursa (job #1655340) | Cod sursa (job #1879408) | Cod sursa (job #2725386) | Cod sursa (job #1389704) | Cod sursa (job #2567253)
#include <fstream>
using namespace std;
ifstream cin ("disjoint.in");
ofstream cout ("disjoint.out");
const int n=100005;
int N,M,tip,x,y;
int tata[n];
int multime(int g)
{
int copieg=g;
while(tata[g]>0)
{
g=tata[g];
}
while(tata[copieg]!=g && tata[copieg] > 0)
{
int aux=tata[copieg];
tata[copieg]=g;
copieg=aux;
}
return g;
}
int main()
{
cin>>N>>M;
for(int i=1;i<=N;++i)
tata[i]=-1;
for(int i=1;i<=M;++i)
{
cin>>tip>>x>>y;
if(tip==2)
{
if (multime(x) == multime(y))
cout << "DA\n";
else cout << "NU\n";
}
if (tip == 1) {
int tatasupremx=multime(x);
int tatasupremy=multime(y);
if (tatasupremx != tatasupremy) {
if (tata[tatasupremx] < tata[tatasupremy]) {
tata[tatasupremx] += tata[tatasupremy];
tata[tatasupremy] = tatasupremx;
}
else {
tata[tatasupremy] += tata[tatasupremx];
tata[tatasupremx] = tatasupremy;
}
}
}
}
return 0;
}