Pagini recente » Cod sursa (job #2648301) | Cod sursa (job #582136) | Cod sursa (job #308510) | Rating Matthewtj (Matthewtj) | Cod sursa (job #2200825)
#include <iostream>
#include <fstream>
#define dMAX 100000
using namespace std;
int n, m, x, y, o, a, b, R;
pair<int, int> arbore[dMAX + 2];
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int Find(int v) {
R = 0;
a = v;
while (arbore[v].first != 0) {
v = arbore[v].first;
}
a = v;
while (arbore[R].first != R) {
a = arbore[R].first;
arbore[R].first = a;
R = a;
}
return a;
}
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;
}