Pagini recente » Cod sursa (job #208352) | Cod sursa (job #2741236) | Cod sursa (job #1378208) | Cod sursa (job #2074606) | Cod sursa (job #2003723)
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int tine, t[100005], nr, operatii, tip, a, b, t1, t2;
int rad(int x)
{
if(t[x] == 0)
return x;
else
{
tine = rad(t[x]);
t[x] = tine;
return tine;
}
}
void modifica(int x, int val)
{
tine = t[x];
t[x] = val;
if(tine == 0)
return;
modifica(tine, val);
}
void uneste(int x, int y)
{
int c = rad(x);
modifica(y, c);
}
int main()
{
cin >> nr >> operatii;
for(int yy=1; yy <= operatii; yy++)
{
cin >> tip >> a >> b;
if(tip == 1)
uneste(a, b);
else
{
t1 = rad(a);
t2 = rad(b);
if(t1 == t2)
cout << "DA\n";
else
cout << "NU\n";
}
}
}