Pagini recente » Cod sursa (job #1379506) | Autentificare | Cod sursa (job #559644) | Cod sursa (job #810451) | Cod sursa (job #3140722)
//#include </Library/Developer/CommandLineTools/usr/include/c++/v1/bits/stdc++.h>
#include <bits/stdc++.h>
using namespace std;
//ifstream fin("/Users/jdev/Documents/ProgrameInfoC++/Disjoint/disjoint.in");
//ofstream fout("/Users/jdev/Documents/ProgrameInfoC++/Disjoint/disjoint.out");
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int root[100001];
int card[100001];
int rooti(int x){
if(root[x] == x) return x;
root[x] = rooti( root[x] );
return root[x];
}
int unire(int x, int y){
if(card[x] < card[y]) swap(x, y);
root[y] = x;
card[x] += card[y];
}
int main(){
//1.
int n, m;
//2.
fin >> n >> m;
//cout << "n = " << n << " m = " << m << endl;
//3.
for(int i = 1; i <= n; i++){
root[i] = i;
card[i] = 1;
}
//cout << endl << endl;
//cout << "n = " << n << " m = " << m << endl;
for(int i = 0; i < m; i++){
int op, x, y; fin >> op >> x >> y;
//cout << "x = " << x << " y = " << y << " root[x] = " << rooti(x) << endl;
if(op == 1){
int r1 = rooti(x);
int r2 = rooti(y);
unire(r1, r2);
}else{
int r1 = rooti(x);
int r2 = rooti(y);
if(r1 == r2) fout << "DA" << endl;
else fout << "NU" << endl;
}
}
return 0;
}