Pagini recente » Cod sursa (job #2130444) | Cod sursa (job #1645864) | Cod sursa (job #404112) | Cod sursa (job #1220824) | Cod sursa (job #1347376)
#include <fstream>
#define DIM 100003
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int Father[DIM];
int n, m;
void Read()
{
in >> n >> m;
for(int i = 1; i <= n; ++i)
Father[i] = i;
}
int Group(int x)
{
if( x != Father[x] )
return Group(Father[x]);
return x;
}
void Unite(int x, int y)
{
x = Group(x), y = Group(y), Father[x] = y;
}
void Solve()
{
int op, x, y;
for(int i = 1; i <= m; ++i)
{
in >> op >> x >> y;
if(op == 1)
Unite(x, y);
else
{
if( Group(x) == Group(y) )
out << "DA\n";
else
out << "NU\n";
}
}
}
int main()
{
Read();
Solve();
return 0;
}