Pagini recente » Cod sursa (job #2638125) | Cod sursa (job #2716510) | Cod sursa (job #2653726) | Cod sursa (job #1006382) | Cod sursa (job #2323310)
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int noduri, operatii, tata[100004], urm, operatie, x, y, poz;
int radacina(int x)
{
poz = x;
while(tata[x] != 0)
x = tata[x];
while(tata[poz] != 0)
{
urm = tata[poz];
tata[poz] = x;
poz = urm;
}
return x;
}
void uneste(int x, int y)
{
y = radacina(y);
while(tata[x] != 0)
{
urm = tata[x];
tata[x] = y;
x = urm;
}
tata[x] = y;
}
int main()
{
cin >> noduri >> operatii;
for(int yy=1; yy <= operatii; yy++)
{
cin >> operatie >> x >> y;
if(operatie == 1)
uneste(x,y);
else
{
if(radacina(x) == radacina(y))
cout << "DA\n";
else
cout << "NU\n";
}
}
}