The Unique MST (判断是否存在多个最小生成树)

                               The Unique MST

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24058   Accepted: 8546

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!
题解:这个题看有的人还求了次小生成树,其实不必要的,中间判断下是否存在多个生成树就好了;代码:
 1 #include<stdio.h>
 2 #include<string.h>
 3 const int INF=0x3f3f3f3f;
 4 const int MAXN=110;
 5 int vis[MAXN],map[MAXN][MAXN],low[MAXN];
 6 int ans,N,answer;
 7 void prime(){
 8     memset(vis,0,sizeof(vis));
 9     int flot=1,temp,k;
10     answer=0;
11     ans=1;
12     vis[1]=1;
13     for(int i=1;i<=N;i++)low[i]=map[1][i];
14     for(int i=1;i<=N;i++){
15         temp=INF;
16         for(int j=1;j<=N;j++)
17         if(!vis[j]&&temp>low[j])temp=low[k=j];
18         if(temp==INF){
19             if(flot!=N)ans=0;//因为这题本来就是联通的,所以这句话可以省略;
20         //    printf("flot=%d,N=%d\n",flot,N);
21             break;
22         }
23         int x=0;
24         for(int j=1;j<=N;j++){
25             if(vis[j]&&map[k][j]==temp)x++;
26             if(x>1)break;
27         }//这个for循环的作用就是,当前已经找到距离这个图最小的点了,然后判断到这个最小点的距离为temp也就是最小值的点有几个,如果点数大于一则有多个最小生成树;
28         if(x>1){
29             ans=0;
30             break;
31         }
32         answer+=temp;
33         vis[k]=1;
34         flot++;
35         for(int j=1;j<=N;j++){
36             if(!vis[j]&&low[j]>map[k][j])low[j]=map[k][j];
37             }
38         }
39     }
40 int main(){
41                 int T,M,a,b,c;
42                 scanf("%d",&T);
43         while(T--){
44             memset(map,INF,sizeof(map));
45                 scanf("%d%d",&N,&M);
46                 while(M--){
47                     scanf("%d%d%d",&a,&b,&c);
48                     if(c<map[a][b])map[a][b]=map[b][a]=c;
49                 }
50                 prime();
51                 if(ans)printf("%d\n",answer);
52                 else printf("Not Unique!\n");
53                     }
54             return 0;
55 }
时间: 2024-08-12 16:23:35

The Unique MST (判断是否存在多个最小生成树)的相关文章

poj1679The Unique MST判断最小生成树是否唯一以及求次小生成树边权和的讲解

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22346   Accepted: 7924 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undirected graph G =

poj 1679 The Unique MST (判断最小生成树是否唯一)

链接:poj 1679 题意:判断最小生成树是否唯一, 若唯一,输出最小权值和,否则,输出  Not Unique! 判断最小生成树是否唯一的思路: 1.对图中的每一条边,扫描其他边,如果存在相同权值的边,则对该边做标记 2.然后用Kruskal算法或Prim算法求MST 3.求得MST后,如果该MST中未包含做了标记的边,即可判断MST唯一: 如果包含做了标记的边,则依次去掉这些边的一条边,再求MST, 如果求得的MST权值和原来的MST的权值一样,即可判断MST不唯一. 个人思路是先求最小生

POJ 1679 The Unique MST 判断最小生成树是否唯一/次小生成树

题目链接: 1679 题意: 给出 M个点N条边 求它的的最小生成树 不唯一则输出:Not Unique! 题解: prim:判断"最小生成树是否唯一"可以理解为"最小生成树和次小生成树是否相等" 求次小生成树的步骤如下 1)先求出最小生成树T,在prim的同时,用一个矩阵maxx[u][v]记录在树中连接u-v的路径中权值最大的边. 2)枚举所有不在T中的边map[u][v],加入边u-v,删除权值为maxx[u][v]的边; 3)找到MST-maxx[u][v]

POJ-1679 The Unique MST (判断最小生成树的唯一性)

<题目链接> 题目大意: 给定一张无向图,判断其最小生成树是否唯一. 解题分析: 对图中每条边,扫描其它边,如果存在相同权值的边,则标记该边:用kruskal求出MST. 如果MST中无标记的边,则该MST唯一:否则,在MST中依次去掉标记的边,再求MST,若求得MST权值和原来的MST 权值相同,则MST不唯一. #include <cstdio> #include <iostream> #include <cstring> #include <al

poj 1679 The Unique MST (判断最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20679   Accepted: 7255 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ--1679--The Unique MST【kruskal判断MST是否唯一】

链接:http://poj.org/problem?id=1679 题意:告诉你有n个点,m条边,以及m条边的信息(起点.终点.权值),判断最小生成树是否唯一 判断MST是否唯一的思路是这样:对于每条边如果有和他相等权值的边,则做一个标记,然后进行一遍kruskal或prim找出最小生成树权值,然后对于每个使用过并且有相等边标记的边,把它从图中删去,再进行一遍kruskal或prim,如果此时最小生成树权值和第一次一样,则说明最小生成树不唯一,否则最小生成树唯一. #include<cstrin

[kuangbin带你飞]专题六 最小生成树 K - The Unique MST (判断最小生成树是否唯一)

K - The Unique MST 题目链接:https://vjudge.net/contest/66965#problem/K 题目: 给定连接的无向图,告诉它的最小生成树是否唯一. 定义1(生成树):考虑连通的无向图G =(V,E). G的生成树是G的子图,比如T =(V',E'),具有以下属性:    1. V'= V.    2.T是连接的和非循环的. 定义2(最小生成树):考虑边加权,连通,无向图G =(V,E). G的最小生成树T =(V,E')是总成本最小的生成树. T的总成本

[2016-01-27][POJ][1679][The Unique MST]

C - The Unique MST Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1679 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree):

POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19941   Accepted: 6999 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire