Pagini recente » Cod sursa (job #2416342) | Cod sursa (job #3234541) | Cod sursa (job #225499) | Cod sursa (job #1927611) | Cod sursa (job #2558042)
#include <fstream>
using namespace std;
ifstream in ("disjoint.in");
ofstream out ("disjoint.out");
int fFind ( int i );
void gluee (int x, int y );
int n, m;
int c, x, y;
int v[100137], r[100137];
int main()
{
in >> n >> m;
for ( register int i = 1 ; i <= n ; ++i )
{
v[i] = i;
r[i] = 1;
}
for ( register int i = 1 ; i <= m ; ++i )
{
in >> c >> x >> y;
if ( c == 1 )
gluee ( x, y );
else
{
if ( fFind (x) == fFind (y) )
out << "DA" << '\n';
else
out << "NU" << '\n';
}
}
return 0;
}
int fFind ( int i )
{
if ( i == v[i] )
return i;
return v[i] = fFind (v[i]);
}
void gluee (int x, int y )
{
int a = fFind (x);
int b = fFind (y);
if ( r[a] < r[b] )
swap(a, b);
v[b] = a;
r[a] += r[b];
}