Pagini recente » Istoria paginii runda/qwerty-3/clasament | Cod sursa (job #2284556) | Cod sursa (job #1282746) | Rating Halati Catalin (hcm90) | Cod sursa (job #2354764)
#include <fstream>
const int MAX_N = 100000;
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int tata[MAX_N + 5], cnt[MAX_N + 5];
int rad(int x) {
if(tata[x] == 0)
return x;
else
tata[x] = rad(tata[x]);
return tata[x];
}
void join(int x, int y) {
x = rad(x);
y = rad(y);
if(x == y)
return;
if(cnt[x] > cnt[y])
swap(x, y);
tata[x] = y;
cnt[y] += cnt[x];
}
int query(int x, int y) {
return (rad(x) == rad(y));
}
int main() {
int n, m;
fin >> n >> m;
for(int i = 1; i <= n; i++) {
tata[i] = 0;
cnt[i] = 1;
}
for(int i = 1; i <= m; i++) {
int op, x, y;
fin >> op >> x >> y;
if(op == 1)
join(x, y);
else
fout << (query(x, y) == 1 ? "DA\n" : "NU\n");
}
}