Pagini recente » Cod sursa (job #1009402) | Cod sursa (job #307865) | Cod sursa (job #1618498) | Cod sursa (job #274698) | Cod sursa (job #3192069)
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <algorithm>
#include <set>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
const int nmax = 200005;
int viz[nmax];
int n, m;
int rang[nmax];
int dad[nmax];
int test, a, b;
int do_find(int nod) {
if (nod != dad[nod]) {
int repr = do_find(dad[nod]);
dad[nod] = repr;
return repr;
}
return nod;
}
void do_union(int x, int y)
{
if (rang[x] < rang[y])
{
dad[x] = y;
}
else if (rang[y] < rang[x])
{
dad[y] = x;
}
else {
dad[x] = y;
rang[y]++;
}
}
int main()
{
f >> n >> m;
for (int i = 1; i <= n; i++)
{
dad[i] = i;
rang[i] = 1;
}
for (int i = 1; i <= m; i++)
{
f >> test >> a >> b;
if (test == 1)
{
do_union(a,b);
}
else {
int repr_a = do_find(a);
int repr_b = do_find(b);
if (repr_a == repr_b)
{
g << "DA\n";
}
else {
g << "NU\n";
}
}
}
}