Pagini recente » Cod sursa (job #1998122) | Cod sursa (job #1744587) | Cod sursa (job #2554928) | Cod sursa (job #2479890) | Cod sursa (job #2181225)
#include <iostream>
#include <fstream>
#define MAX 100001
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, rang[MAX], traseu[MAX];
void init()
{
for (int i = 1; i <= n; ++i)
rang[i] = traseu[i] = i;
}
/*
int Find(int x)
{
int aux, y;
//merg in sus pe arbore pana gasesc un nod care pointeaza catre el insusi
for (aux = x; traseu[aux] != aux; aux = traseu[aux]);
//aplic compresia drumurilor
while (traseu[x] != x) {
y = traseu[x];
traseu[x] = aux;
x = y;
}
return aux;
}
void unite(int x, int y)
{
if (rang[x] > rang[y])
traseu[y] = x;
else
traseu[x] = y;
if (rang[x] == rang[y])
rang[y]++;
}
*/
int Find(int x){
if (x == traseu[x])
return x;
return Find(traseu[x]);
}
void unite(int x, int y){
x = Find(x);
y = Find(y);
if (rang[x] > rang[y])
swap(x, y);
traseu[x] = y;
rang[y] += rang[x];
}
int main()
{
int x, y, cod;
fin >> n >> m;
init();
for (int i = 0; i < m; ++i) {
fin >> cod >> x >> y;
if (cod == 1) {
unite(Find(x), Find(y));
}
else {
if (Find(x) == Find(y))
fout << "DA\n";
else
fout << "NU\n";
}
}
fout.close();
return 0;
}