Pagini recente » Istoria paginii runda/alex001/clasament | Cod sursa (job #2571744) | Cod sursa (job #441128) | Istoria paginii runda/oni2011_9_1/clasament | Cod sursa (job #1398958)
#include <fstream>
using namespace std;
ifstream is("disjoint.in");
ofstream os("disjoint.out");
int n, m, cod, x, y, a[100002];
void SetRoot();
int GetRoot(int x);
void Merge(int x, int y);
int main()
{
is >> n >> m;
SetRoot();
for ( int i = 0; i < m; ++i )
{
is >> cod >> x >> y;
if ( cod == 1 )
Merge(x, y);
else if ( GetRoot(x) == GetRoot(y) )
os << "DA\n";
else
os << "NU\n";
}
is.close();
os.close();
return 0;
}
void SetRoot()
{
for ( int i = 1; i <= n; ++i )
a[i] = i;
}
int GetRoot(int x)
{
if ( a[x] == x )
return x;
return GetRoot(a[x]);
}
void Merge(int x, int y)
{
int n1 = GetRoot(x);
int n2 = GetRoot(y);
a[n1] = n2;
}