Pagini recente » Cod sursa (job #1373109) | Cod sursa (job #2550328) | Cod sursa (job #497260) | Cod sursa (job #2140421) | Cod sursa (job #2199345)
#include <iostream>
#include <fstream>
#define dMAX 100000
using namespace std;
int n, m, x, y, o;
pair<int, int> arbore[dMAX + 2];
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int Find(int v) {
while (arbore[v].first != 0) {
v = arbore[v].first;
}
return v;
}
void Union(int a, int b) {
if (arbore[a].second > arbore[b].second) {
arbore[b].first = a;
} else arbore[a].first = b;
if (arbore[a].second == arbore[b].second)
arbore[b].second++;
}
int main()
{
int i, j;
fin >> n >> m;
for (i = 1; i <= m; i++) {
fin >> o >> x >> y;
if (o == 1) {
Union(Find(x), Find(y));
} else {
if (Find(x) == Find(y)) {
fout << "DA\n";
} else fout << "NU\n";
}
}
return 0;
}