Pagini recente » Cod sursa (job #2387444) | Cod sursa (job #1337392) | Cod sursa (job #1227502) | Cod sursa (job #1465477) | Cod sursa (job #2946776)
#include<bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in");
ofstream o("disjoint.out");
int N, M;
int tati[100001];
int tata(int &x) {
while (tati[x] != x) {
tati[x] = tati[tati[x]];
x = tati[x];
}
return x;
}
void reuniune(int &x, int &y) {
if (x > y) tati[tata(x)] = tata(y);
else tati[tata(y)] = tata(x);
}
int main() {
f>>N>>M;
for (int i = 0; i <= M; ++i) tati[i] = i;
for (int i = 0; i < M; ++i) {
int op, x, y;
f>>op>>x>>y;
if (op == 1) reuniune(x, y);
else if (tata(x) == tata(y)) o<<"DA\n";
else o<<"NU\n";
}
f.close();
o.close();
return 0;
}