#include #include int numVertices,numEdges; int *parent,*weight,numTrees; int *bestEdgeNum; struct edge { int tail,head,weight; }; typedef struct edge edgeType; edgeType *edgeTab; int find(x) int x; { int i,j,root; for (i=x; parent[i]!=i; i=parent[i]) ; root=i; /* path compression */ for (i=x; parent[i]!=i; j=parent[i],parent[i]=root,i=j) ; return root; } void makeEquivalent(i,j) int i,j; { if (weight[i]>weight[j]) { parent[j]=i; weight[i]+=weight[j]; } else { parent[i]=j; weight[j]+=weight[i]; } numTrees--; } int main() { int i,MSTweight=0; int root1,root2; int usefulEdges; scanf("%d %d",&numVertices,&numEdges); edgeTab=(edgeType*) malloc(numEdges*sizeof(edgeType)); parent=(int*) malloc(numVertices*sizeof(int)); weight=(int*) malloc(numVertices*sizeof(int)); bestEdgeNum=(int*) malloc(numVertices*sizeof(int)); if (!edgeTab || !parent || !weight || !bestEdgeNum) { printf("error 2\n"); exit(0); } for (i=0;i1 && usefulEdges>0) { for (i=0;iedgeTab[i].weight) bestEdgeNum[root1]=i; // Have a new best edge from this component if (bestEdgeNum[root2]==(-1) || edgeTab[bestEdgeNum[root2]].weight>edgeTab[i].weight) bestEdgeNum[root2]=i; // Have a new best edge from this component } } for (i=0;i