Pagini recente » Cod sursa (job #2327763) | Cod sursa (job #2397133) | Cod sursa (job #2800459) | Cod sursa (job #1669299) | Cod sursa (job #3030209)
#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], h[N];
int radacina(int x){
if(t[x] == 0)
return x;
t[x] = radacina(t[x]); // compactarea drumurilor <=> legam toate nodurile prin care trecem direct de radacina
return t[x];
}
void reuniune(int x, int y){
int rx = radacina(x);
int ry = radacina(y);
if(h[rx] > h[ry]){
t[ry] = rx;
//
}else{
t[rx] = ry;
h[ry] = max(h[ry], h[rx] + 1); // practic doar pt. cazul in care h[rx] == h[ry]
}
}
inline bool verif(int x, int y){
return radacina(x) == radacina(y);
}
int main(){
f >> n >> m;
for(int i = 1; i <= n; i++)
h[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();
}