Pagini recente » oji.2011 | tema | Votati Zaharel | Istoria paginii runda/7778/clasament | Cod sursa (job #1398962)
#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;
int f = GetRoot(a[x]);
a[x] = f;
return f;
}
void Merge(int x, int y)
{
int n1 = GetRoot(x);
int n2 = GetRoot(y);
a[n1] = n2;
}