You are viewing a single comment's thread. Return to all comments →
#include <cstdlib> #include<iostream> using namespace std; /* * */ const int MAX=1e5+6; int id[MAX]; int s[MAX]; void init(){ for(int i=0;i<MAX;i++){ id[i]=i; s[i]=1; } } long long int findroot(long long int i){ while(i!=id[i]){ id[i]=id[id[i]]; i=id[i]; } return i; } void weightedu(long long int x ,long long int y){ long long int i=findroot(x); long long int j=findroot(y); id[j]=i; s[i]=s[i]+s[j]; s[j]=0; } int main() { long long int i ,j ,nodes ,edges ,x,y ,f[MAX]={0} ,p=1 ,sum=0; init(); cin>>nodes>>edges; for(i=0;i<edges;i++){ cin>>x>>y; weightedu(x ,y); } for(i=0;i<nodes;i++){ for(j=i+1;j<nodes;j++){ if(s[j]!=0){ p=s[i]*s[j]; sum=sum+p; } } } cout<<sum; }
Journey to the Moon
You are viewing a single comment's thread. Return to all comments →