Pagini recente » Cod sursa (job #1710052) | Cod sursa (job #1601668) | Cod sursa (job #1882644) | Cod sursa (job #2441892) | Cod sursa (job #1196765)
#include <fstream>
using namespace std;
#define dim 100005
int P[dim],rank[dim];
void CREATE_SETS(int n){
for(int i=1;i<=n;i++){
rank[i]=1;
P[i]=i;
}
}
int FIND_SET(int x){
if(P[x]!=x) P[x]=FIND_SET(P[x]);
return P[x];
}
void MERGE_SETS(int x,int y){
if(rank[x]>=rank[y])
P[y]=x;
else P[x]=P[y];
if(rank[x]==rank[y]) rank[y]++;
}
int main(){
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m,t,x,y,i;
f >> n >> m;
CREATE_SETS(n);
for(i=1;i<=m;i++){
f >> t >> x >> y;
if(t==1){
MERGE_SETS(x,y);
}
else{
if(FIND_SET(x)==FIND_SET(y)) g << "DA\n";
else g << "NU\n";
}
}
}