博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
The Unique MST (判断是否存在多个最小生成树)
阅读量:6279 次
发布时间:2019-06-22

本文共 2765 字,大约阅读时间需要 9 分钟。

                               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

23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2

Sample Output

3Not Unique!题解: 这个题看有的人还求了次小生成树,其实不必要的,中间判断下是否存在多个生成树就好了; 代码:
1 #include
2 #include
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

 

转载地址:http://upyva.baihongyu.com/

你可能感兴趣的文章
吐槽身边互联网那些事儿!
查看>>
搭建自己的OpenWrt开发环境
查看>>
Redis系列一之数据结构
查看>>
qt4升级到qt5的一些问题
查看>>
Working with SQL Server LocalDB
查看>>
Redis 配置
查看>>
■ apache隐藏、混淆php后缀名的几种方法
查看>>
python基础之元组、文件操作、编码、函数、变量
查看>>
NOIP1999邮票面值设计[搜索|DP]
查看>>
gnuplot使用1
查看>>
<转>SQL语句大全
查看>>
[Javascript] Either Functor
查看>>
搭建一个分布式MongoDB鉴权集群
查看>>
Jquery 对象集合的迭代扩展forEach
查看>>
看懂理解 keyboard中 , navigation的设置: 切换工作区和移动窗口到不同的工作区.
查看>>
mysqldump和xtrabackup备份原理实现说明
查看>>
[Angular2 Form] Create and Submit an Angular 2 Form using ngForm
查看>>
Atitit.数据检索与网络爬虫与数据采集的原理概论
查看>>
POJ3494Largest Submatrix of All 1’s[单调栈]
查看>>
ofstream的使用方法
查看>>