Pagini recente » Cod sursa (job #430248) | Cod sursa (job #2590453) | Cod sursa (job #479495) | Cod sursa (job #43481) | Cod sursa (job #2807203)
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
namespace DSU {
int dsu[100000];
int area[100000];
void init(int n) {
for(int i=0; i<n; i++) {
dsu[i]=i;
area[i]=1;
}
}
int father(int x) {
return (dsu[x]==x? x : father(dsu[x]=father(dsu[dsu[x]])));
}
void unite(int x, int y) {
x=father(x);
y=father(y);
if(area[x]<area[y])
swap(x,y);
area[x]+=area[y];
dsu[y]=x;
}
};
int main() {
int n,m;
cin >> n >> m;
DSU::init(n);
for(int i=0,x,y,t; i<m; i++) {
cin >> t >> x >> y;
--x;
--y;
if(t==1) {
DSU::unite(x,y);
}
else {
cout << (DSU::father(x)==DSU::father(y)? "DA\n" : "NU\n");
}
}
}