Pagini recente » Cod sursa (job #2560527) | Cod sursa (job #1627446) | Cod sursa (job #1825857) | Cod sursa (job #2519290) | Cod sursa (job #3140719)
#include </Library/Developer/CommandLineTools/usr/include/c++/v1/bits/stdc++.h>
using namespace std;
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 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){
if(card[x] < card[y]) swap(x, y);
card[x] += card[y];
root[y] = x;
}else{
int r1 = rooti(x);
int r2 = rooti(y);
if(r1 == r2) fout << "DA" << endl;
else fout << "NU" << endl;
}
}
return 0;
}