Pagini recente » Cod sursa (job #2771221) | Cod sursa (job #584113) | Cod sursa (job #74113) | Cod sursa (job #1021434) | Cod sursa (job #2360916)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream in ("disjoint.in");
ofstream out ("disjoint.out");
const int N = 100001;
int nr[N];
int t[N];
int radacina ( int x )
{
if ( t[x] == 0 )
{
return x;
}
t[x] = radacina(t[x]);
return t[x];
}
bool verif ( int x, int y )
{
return ( radacina(x) == radacina(y) );
}
void reuniune (int x, int y)
{
int rx = radacina (x);
int ry = radacina (y);
if ( rx == ry)
{
return;
}
if ( nr[rx] < nr[ry] )
{
nr[ry]+= nr[rx];
t[rx] = ry;
}
else
{
nr[rx]+= nr[ry];
t[ry] = rx;
}
}
int main ()
{
int n, m;
in >> n >> m;
while ( m != 0 )
{
int op, x, y;
in >> op >> x >> y;
if ( op == 1 )
{
reuniune (x, y);
}
else
{
if ( radacina(x) == radacina(y) )
{
out << "DA\n";
}
else
{
out << "NU\n";
}
}
m--;
}
return 0;
}