poj1679——The Unique MST【最小生成树,次小生成树】

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 34215   Accepted: 12458

Description

Given a connected undirected graph, tell if its minimum spanning tree is unique. 

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties: 
1. V' = V. 
2. T is connected and acyclic. 

Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'. 

Input

The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

Output

For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

Sample Input

2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2

Sample Output

3
Not Unique!

Source

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di

Any problem, Please Contact Administrator

次小生成树模板题,这道题是在网上看的模板,然后自己又写了一遍,不过代码的核心思想还是没理解,先把模板放着方便以后理解查看:

  
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int pre[100010];
int n,m,cnt; 
bool flag;
struct node{
	int u,v,w;
	int eq,used,del;
}temp[100010];
bool cmp(node a,node b){
	return a.w<b.w;
}
void init(){
	for(int i=1;i<=10005;i++){
		pre[i]=i;
	}
}
int find(int x){
	if(x==pre[x]) return pre[x];
	else{
		pre[x]=find(pre[x]);
		return pre[x];
	}
}
int join(int x,int y){
	int fx=find(x),fy=find(y);
	if(fx!=fy){
		pre[fy]=fx;//把x归并到y的集合中
		return 1; 
	}
	return 0;
}
int  kruskal(){
	init();
	int sum=0,num=0;
	for(int i=0;i<=m;i++){
		if(temp[i].del==1) continue;//如果有重边则跳过
		int u=temp[i].u,v=temp[i].v,w=temp[i].w;
		if(join(u,v)){
			sum=sum+w;
			if(!flag) temp[i].used=1;
			num++;
		} 
		if(num==n-1) break;
	}
	return sum;
}
int main() {
    int t,d;
    cin>>t;
    while(t--) {
        cnt=0;
        cin>>n>>m;
        for(int i=0; i<m; i++) {
            cin>>temp[i].u>>temp[i].v>>temp[i].w;
            temp[i].del=0;
            temp[i].used=0;
            temp[i].eq=0;//一开始这个地方eq没有初始化,WA了好几发,操
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<m;j++){
                if(i==j)continue;
                if(temp[i].w==temp[j].w)temp[i].eq=1;
            }
        }
        sort(temp,temp+m,cmp);
        flag=false;
        cnt=kruskal();
        flag=true;
        bool gg=false;
        for(int i=0;i<m;i++){
            if(temp[i].used==1&&temp[i].eq==1){
                temp[i].del=1;
                int s=kruskal();//printf("%d %d\n",i,s);
                if(s==cnt){
                    gg=true;
                    printf("Not Unique!\n");
                    break;
                }
                temp[i].del=0;
            }
        }
        if(!gg)cout<<cnt<<endl;
    }
    return 0;
}
思路是先判断每条边是否有重边,有的话eq=1,否则0.然后第一次求出最小生成树,将结果记录下来,
然后依次去掉第一次使用过的且含有重边的边,再求一次最小生成树,若结果与第一次结果一样,则不唯一。

猜你喜欢

转载自blog.csdn.net/xiang_hehe/article/details/80155325