Pagini recente » Borderou de evaluare (job #2135477) | Borderou de evaluare (job #2016367) | Borderou de evaluare (job #1731295) | Borderou de evaluare (job #1221323) | Cod sursa (job #3127704)
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
const int dim = 1e5+5;
int h[dim],t[dim],n,m;
void unionf(int x, int y) {
if(h[x]>h[y])
t[y] = x;
else
if(h[y]>h[x])
t[x] = y;
else if(h[x]==h[y]){
t[x] = y;
h[y]++;
}
}
int find(int x) {
if(t[x] == x)
return x;
t[x] = find(t[x]);
}
int main() {
cin >> n;
for(int i = 1; i <= n; ++i)
t[i] = i;
cin >> m;
for(int i = 1,test,x,y; i <= m; ++i) {
cin >> test >> x >> y;
if(test == 1)
unionf(find(x),find(y));
else
if(find(x) == find(y))
cout << "DA\n";
else cout << "NU\n";
}
}