111

#include<stdio.h>
#include<stdlib.h>
#define Confi double//set Confidence as double 
#define Vertex int//set Vertex as int 
#define Weight double//set Weight as double 
struct Edge{//one edge has three properties-two vertexes and the weight
	Vertex V1;
	Vertex V2;
	Weight W;
};

struct Component{//one Component has three properties 
	int Num;//Num stands for the (-size) when it's the root, else it points to its fahter
    Weight MaxEdge;//its maximum edge weight of the minimum spanning tree
    Confi H;//its Confidence
};

Confi Min(Confi H1,Confi H2){//to find the smaller Confidence
	if (H1>H2)
	return H2;
	else return H1;
}

Vertex FindRoot(Vertex V,struct Component C[]){//the find a Vertex's root 
	if(C[V].Num<0) return V;      //found
	else
	{
		C[V].Num=FindRoot(C[V].Num,C);//path compression
		return C[V].Num;          
	}
}

int main(){
	int Nv=0,Ne=0;double c=0;
	printf("Please enter the total number of vertices, the total number of edges and the constant in the function f(C).\n");
	scanf("%d %d %lf",&Nv,&Ne,&c);//to get three parameters 
	
	struct Edge E[Ne];
	int i=0;int j=0;
	for(i=0;i<Ne;i++)
	scanf("%d %d %lf",&E[i].V1,&E[i].V2,&E[i].W);//to get the Edge
	printf("Please enter the edge in the format of 'V1 V2 Weight'.\n");
	struct Edge temp;//order Edge in a non-decreasing sequence 
	for(i=1;i<Ne;i++)
       for(j=0;j<Ne-i;j++)
	      if(E[j].W>E[j+1].W) {
	         temp= E[j];
	         E[j]=E[j+1];
	         E[j+1]=temp;
	      }
	
	struct Component C[Nv];//initialize the Component(at first, every vertex is a component) 
	for(i=0;i<Nv;i++){
		C[i].Num=-1;//every conponent has one vertex
		C[i].MaxEdge=0;//its maximum edge weight of the minimum spanning tree is 0
		C[i].H=c;//its confidence equals to c
	}
    int Root1,Root2;int RootMin,RootMax;
    for(i=0;i<Ne;i++){
    Root1=FindRoot(E[i].V1,C);//to find one root
	Root2=FindRoot(E[i].V2,C);//to find another root
	if((Root1!=Root2)&&(E[i].W<=Min(C[Root1].H,C[Root2].H))){
		//if the root is different&&
		//the weight is no larger than the smaller confidence of the two components
	if(Root1>Root2){//to get the smaller root and the larger root
		RootMin=Root2;RootMax=Root1;
	}
	else{
		RootMin=Root1;RootMax=Root2;
	} 
	C[RootMin].MaxEdge=E[i].W;//renew the maximum edge weight of the minimum spanning tree 
	C[RootMin].Num=C[RootMin].Num+C[RootMax].Num;//renew the size of the component
	C[RootMin].H=C[RootMin].MaxEdge+(-c)/C[RootMin].Num;//renew the confidence
	C[RootMax].Num=RootMin;//the larger root points to the new root
	}}
	
	Vertex Root[Nv];//create the root array 
	for(i=0;i<Nv;i++)
	Root[i]=FindRoot(i,C);//the array value is the  root
	
	for(i=0;i<Nv;i++){//begin to output from 0  
		if(Root[i]!=Nv){//if the vertex hasn't been outputed
			printf("%d",i);//we output it 
			for(j=i+1;j<Nv;j++){//then to find the vertex which has the same root
				if(Root[j]==Root[i]){//if the root is the same 
				Root[j]=Nv;//we change its root into Nv 
				printf(" %d",j);}//then we output it 
			}printf("\n");
		}}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值