Pagini recente » Cod sursa (job #1254191) | Cod sursa (job #814106) | Cod sursa (job #1179771) | Cod sursa (job #1758254) | Cod sursa (job #3001549)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(WHATEVER) for(int i = 1; i <= WHATEVER; ++ i)
/// INPUT / OUTPUT
const string problem = "disjoint";
ifstream fin(problem + ".in");
ofstream fout(problem + ".out");
/// GLOBAL VARIABLES
const ll NMAX = 1e5+5, MOD = 1e9 + 7, INF = 1e9;
int n, q, sets[NMAX], parent[NMAX];
/// SOLUTION
inline int Find(int x)
{
if(parent[x] != x)
return Find(parent[x]);
else
return x;
}
inline void Union(int x, int y)
{
if(x == y)
return;
if(x > y)
swap(x ,y);
sets[x]+= sets[y];
parent[y] = parent[x];
}
/// READING THE INPUT
int main()
{
ios::sync_with_stdio(false);
fin.tie(NULL);
fout.tie(NULL);
fin >> n >> q;
for(int i = 1; i <= n; ++ i)
{
sets[i] = 1;
parent[i] = i;
}
while(q--)
{
int tip, x, y;
fin >> tip >> x >> y;
if(tip == 1)
{
Union(Find(x), Find(y));
}
else
{
if(Find(x) == Find(y))
fout << "DA" << '\n';
else
fout << "NU" << '\n';
}
}
}