Pagini recente » Cod sursa (job #1594376) | Cod sursa (job #2648442) | Cod sursa (job #2320125) | Cod sursa (job #1581044) | Cod sursa (job #2686412)
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n, m;
int t[100005], d[100005];
int tip, x, y;
int opParc(int x){
if(t[x] == x){
return x;
}
int ajut = opParc(t[x]);
t[x] = ajut;
return ajut;
}
void init(){
for(int i=1; i<=n; i++)
t[i] = i;
}
int main()
{
f>>n>>m;
init();
for(int i=0; i<m; i++){
f>>tip>>x>>y;
if(tip == 2){
if(opParc(x) != opParc(y))
g<<"NU"<<'\n';
else
g<<"DA"<<'\n';
}
else{
int t1 = opParc(x), t2 = opParc(y);
if(d[t1] == d[t2]){
t[t2] = t1;
d[t1] ++ ;
}
else if(d[t1] < d[t2]){
t[t1] = t2;
}
else{
t[t2] = t1;
}
}
}
return 0;
}