Pagini recente » Cod sursa (job #2366232) | Cod sursa (job #351538) | Cod sursa (job #1312831) | Cod sursa (job #2556969) | Cod sursa (job #2945297)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout ("disjoint.out");
vector<int> t;
int find(int x){
while(x != t[x]){
x = t[x];
}
return x;
}
void op1(int x, int y){
int x1, y1;
x1 = find(x);
y1 = find(y);
t[y1] = x1;
}
void op2(int x, int y){
if (find(x) == find(y)){
fout<<"DA"<<endl;
}
else
fout<<"NU"<<endl;
}
int main() {
int n, m, cod, x, y;
fin>>n>>m;
t.resize(n + 1);
for (int i = 1; i < n + 1; ++i) {
t[i] = i;
}
for (int i = 0; i < m; ++i) {
fin>>cod>>x>>y;
if (cod == 1) {
op1(x,y);
}
else {
op2(x,y);
}
}
return 0;
}