Pagini recente » Cod sursa (job #1333331) | Cod sursa (job #1615352) | Cod sursa (job #1164668) | Cod sursa (job #1134929) | Cod sursa (job #2973408)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, tt[100001], rg[100001];
int cauta(int x)
{
int i, y;
for (i = x; tt[i] != i; i = tt[i]) {}
while (tt[x] != x) {
y = tt[x];
tt[x] = i;
x = y;
}
return i;
}
void uneste(int x, int y)
{
if (rg[x] > rg[y])
tt[y] = x;
else
tt[x] = y;
if (rg[x] == rg[y])
rg[y]++;
}
int main()
{
int x, y, i, cod, a, b;
fin >> n >> m;
for (i = 1; i <= n; i++) {
tt[i] = i;
rg[i] = 1;
}
for (i = 1; i <= m; i++) {
fin >> cod >> x >> y;
a = cauta(x);
b = cauta(y);
if (cod == 2) {
if (a == b)
fout << "DA" << "\n";
else
fout << "NU" << "\n";
}
else
uneste(a, b);
}
return 0;
}