here is only basic implementation of problems for beginners. If you have any problem with any solution or any basic concept of programming or you want more efficient solution you can mail me.
my suggestion is not to copy and paste codes from here try to understand the logic and think why you were not able to solve it.

Friday 12 September 2014

Spoj world and contients

#include<stdio.h>
int parent[100002];
int find(int i){
    if(parent[i]!=i)
        parent[i]=find(parent[i]);
    return parent[i];
}
int main(){
int a,b,c,x,i,m,n;
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++){
parent[i]=i;
}
x=n;
while(m--){
scanf("%d %d",&b,&c);
int b_root=find(b);
    int c_root=find(c);
    if(b_root!=c_root){
            parent[b_root]=c_root;
            x--;
    }
}
printf("%d\n",x-1);
return 0;
}

No comments:

Post a Comment