Pagini recente » Cod sursa (job #3000954) | Cod sursa (job #229349) | Cod sursa (job #527297) | Cod sursa (job #835437) | Cod sursa (job #2861360)
#include <bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
const int N = 1e5 + 1;
int n, m, tip, x, y;
int t[N], nr[N];
int radacina(int x){
if(t[x] == 0)
return x;
t[x] = radacina(t[x]);
return t[x];
}
void reuniune(int x, int y){
int rx = radacina(x);
int ry = radacina(y);
if(nr[rx] > nr[ry]){
t[ry] = rx;
nr[rx] += nr[ry];
}else{
t[rx] = ry;
nr[ry] += nr[rx];
}
}
inline bool verif(int x, int y){
return radacina(x) == radacina(y);
}
int main(){
f >> n >> m;
for(int i = 1; i <= n; i++)
nr[i] = 1;
while(m--){
f >> tip >> x >> y;
if(tip == 1)
reuniune(x, y);
else{
if(verif(x, y))
g << "DA\n";
else
g << "NU\n";
}
}
f.close();
g.close();
}