Pagini recente » Cod sursa (job #643997) | Cod sursa (job #583060) | Cod sursa (job #638117) | Cod sursa (job #1153174) | Cod sursa (job #3190658)
#include <bits/stdc++.h>
using namespace std;
int n,q;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
struct DSU {
int n;
vector<int> t, sz;
DSU(int _n){
n = _n;
t.resize(n+1);
sz.resize(n+1);
for(int i = 1; i <= n; i++){
t[i] = i;
sz[i] = 1;
}
}
int root(int nod){
if(t[nod] == nod){
return nod;
}
return t[nod] = root(t[nod]);
}
void join(int x, int y){
x = root(x);
y = root(y);
if(sz[x] < sz[y]){
swap(x, y);
}
t[y] = x;
sz[x] += sz[y];
}
bool areJoined(int x, int y){
return (root(x) == root(y));
}
};
int main()
{
fin >> n >> q;
DSU ds(n);
while(q--){
int t,x,y;
fin >> t >> x >> y;
if(t == 1){
ds.join(x, y);
}else{
fout << (ds.areJoined(x, y) ? "DA" : "NU") << "\n";
}
}
return 0;
}