POJ1679次小生成树

判断最小生成树是不是唯一。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int maxn=110,maxm=10010,INF=100000000;
struct node{
	int x,y,w,f;
};
struct node a[maxm],tmp;
int f[maxn],rank[maxn],pre[maxn],pres[maxn],p[maxn],n,ansx[maxm];
int e,to[maxm],next[maxm],begin[maxn],ss[maxm];
void qsort(int l,int r){
	int mid=a[(l+r)/2].w,i,j;
	i=l;j=r;
	while(i<j){
		while(i<=j && a[i].w<mid)i++;
		while(i<=j && a[j].w>mid)j--;
		if(i<=j){
			tmp=a[i];a[i]=a[j];a[j]=tmp;
			i++;j--;
		}
	}
	if(l<j)qsort(l,j);
	if(i<r)qsort(i,r);
}
int fa(int u){
	if(f[u]!=u)
		f[u]=fa(f[u]);
	return f[u];
}
int find(int u,int v){
	return fa(u)==fa(v)?0:1;
}
void link(int u,int v){
	if(rank[fa(u)]>=rank[fa(v)]){
		f[fa(v)]=fa(u);
		rank[fa(u)]++;
	}else{
		f[fa(u)]=fa(v);
		rank[fa(v)]++;
	}
}
void dfs(int u,int v){
	int i,j,k;
	if(u==v)return;
	p[u]=1;
	for(i=begin[u];i;i=next[i]){
		k=to[i];
		if(!p[k]){
			pre[k]=u;
			pres[k]=ss[i];			
			dfs(k,v);
		}
	}
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("prim2.in","r",stdin);
	//freopen("cprim.out","w",stdout);
#endif
	int i,j,k,m,s;
int T;
scanf("%d",&T);
while(T--){
	memset(f,0,sizeof(f));
	memset(rank,0,sizeof(rank));
	memset(p,0,sizeof(p));
	memset(begin,0,sizeof(begin));
	memset(next,0,sizeof(next));
	e=0;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)f[i]=i;
	for(i=1;i<=m;i++){
		scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);		
		a[i].f=0;
	}
	qsort(1,m);
	s=n-1;i=0;
	int ans=0;
	while(s && i<=m){
		i++;
		if(find(a[i].x,a[i].y)){
			link(a[i].x,a[i].y);
			
			to[++e]=a[i].y;
			next[e]=begin[a[i].x];
			begin[a[i].x]=e;
			ss[e]=a[i].w;
			to[++e]=a[i].x;
			next[e]=begin[a[i].y];
			begin[a[i].y]=e;
			ss[e]=a[i].w;			
			
			a[i].f=1;
			s--;
			ans+=a[i].w;			
		}		
	}
	int t=0;
	for(i=1;i<=m;i++){
		if(a[i].f==0){			
			memset(p,0,sizeof(p));
			dfs(a[i].x,a[i].y);
			j=a[i].y;
			int max=0;			
			while(j!=a[i].x){
				if(pres[j]>max)
					max=pres[j];		
				j=pre[j];
			}				
			ansx[++t]=ans-max+a[i].w;			
		}
				
	}	

	int flag=1;
	for(i=1;i<=t;i++)
		if(ansx[i]==ans){
			flag=0;
			break;
		}
	
	if(flag)printf("%d\n",ans);
	else puts("Not Unique!");
}
	return 0;
}

你可能感兴趣的:(POJ1679次小生成树)