Pagini recente » Cod sursa (job #130001) | Cod sursa (job #2270120) | Cod sursa (job #625103) | Cod sursa (job #1487932) | Cod sursa (job #2919135)
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
string msg[2] = {"NU\n", "DA\n"};
const int MAX_N = 100005;
int n, q, type, x, y;
int tata[MAX_N];
static inline int root(int nod){
int cpy, rad;
rad = nod;
while(tata[rad] != 0)
rad = tata[rad];
while(tata[nod] != 0){
cpy = nod;
nod = tata[nod];
tata[cpy] = rad;
}
return rad;
}
static inline void join(int x, int y){
int rx = root(x);
int ry = root(y);
if(rx != ry)
tata[rx] = ry;
}
int main (){
ios_base::sync_with_stdio(false);
fin.tie(nullptr), fout.tie(nullptr);
fin>>n>>q;
while(q--){
fin>>type>>x>>y;
if(type == 1) /// join
join(x, y);
else /// query
fout<<msg[root(x) == root(y)];
}
return 0;
}