Pagini recente » Cod sursa (job #860896) | Cod sursa (job #1070355) | Cod sursa (job #1582152) | Cod sursa (job #586524) | Cod sursa (job #2201401)
#include <iostream>
#include <fstream>
#include <string.h>
#define dMAX 100000
using namespace std;
int n, m, x, y, o, idx, t;
pair<int, int> arbore[dMAX + 2];
char s[35];
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;
fin.get();
for (i = 1; i <= m; i++) {
fin.getline(s, 35);
idx = 0;
t = 0;
while (isdigit(s[idx])) {
t *= 10;
t += (int)s[idx] - '0';
idx++;
}
idx++;
o = t;
t = 0;
while (isdigit(s[idx])) {
t *= 10;
t += (int)s[idx] - '0';
idx++;
}
idx++;
x = t;
t = 0;
while (isdigit(s[idx])) {
t *= 10;
t += (int)s[idx] - '0';
idx++;
}
y = t;
if (o == 1) {
Union(Find(x), Find(y));
} else {
if (Find(x) == Find(y)) {
fout << "DA\n";
} else fout << "NU\n";
}
}
return 0;
}