Pagini recente » Cod sursa (job #2237659) | Cod sursa (job #2035952) | Cod sursa (job #622563) | Cod sursa (job #302771) | Cod sursa (job #1347372)
#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)
{
Father[Group(x)] = Group(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;
}