Pagini recente » Cod sursa (job #2443753) | Cod sursa (job #2313677) | Cod sursa (job #1084824) | Cod sursa (job #2408397) | Cod sursa (job #2772190)
#include <fstream>
#define NMAX 100005
using namespace std;
/*************************************/
/// INPUT / OUTPUT
ifstream f("disjoint.in");
ofstream g("disjoint.out");
/*************************************/
/// GLOBAL DECLARATIONS
int N, Q;
int height[NMAX], par[NMAX];
/*************************************/
/// FUNCTIONS
void ReadInput();
void Solution();
/*************************************/
///----------------------------------------------
inline void ReadInput()
{
f >> N >> Q;
for (int i = 1 ; i <= N ; ++ i)
{
par[i] = i;
height[i] = 1;
}
}
///----------------------------------------------
int Find(int node)
{
if (node == par[node])
return node;
par[node] = Find(par[node]);
return par[node];
}
///----------------------------------------------
void Union(int a, int b)
{
int parA = Find(a);
int parB = Find(b);
if (height[parA] < height[parB])
{
swap(a, b);
swap(parA, parB);
}
height[parA] += height[parB];
par[parB] = parA;
}
///----------------------------------------------
inline void Solution()
{
while (Q --)
{
int p, a, b;
f >> p >> a >> b;
if (p == 1)
Union(a, b);
else
if (Find(a) == Find(b))
g << "DA" << "\n";
else
g << "NU" << "\n";
}
}
///----------------------------------------------
int main()
{
ReadInput();
Solution();
return 0;
}