POJ1679:The Unique MST(次小生成树)
来源:互联网 发布:北京农家院出售 知乎 编辑:程序博客网 时间:2024/04/29 13:39
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'.
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!
题意:判断最小生成树是否唯一
思路:首先求出最小生成树,记录现在这个最小生成树上所有的边,然后通过取消其中一条边,找到这两点上其他的边形成一棵新的生成树,求其权值,通过枚举所有可能,通过这些权值看与原最小生成树的权值比较看其是否唯一
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;struct node{ int x,y,dis; int flag;} a[10005];int cmp(node x,node y){ return x.dis<y.dis;}int father[105],n;int kruskal(int num,int m){ int i,j,k; int ans = 0,cnt = 1; for(i = 0; i<m; i++) { if(i == num)//除去这条边之后再求一次最小生成树 continue; int s1 = father[a[i].x]; int s2 = father[a[i].y]; if(s1!=s2) { ans+=a[i].dis; cnt++; father[s2] = s1; for(j = 0; j<=n; j++) if(father[j] == s2) father[j] = s1; } } if(cnt!=n) return -1; else return ans;}int main(){ int m,i,j,t,sum,ans,cnt; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i = 0; i<=n; i++) father[i] = i; for(i = 0; i<m; i++) { scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].dis); a[i].flag = 0; } sort(a,a+m,cmp); cnt = 1; ans = 0; for(i = 0; i<m; i++) { int s1 = father[a[i].x]; int s2 = father[a[i].y]; if(s1!=s2) { a[i].flag = 1; ans+=a[i].dis; cnt++; father[s2] = s1; for(j = 0; j<=n; j++) if(father[j] == s2) father[j] = s1; } } int flag = 0; for(i = 0; i<m; i++)//枚举所有原最小生成树上的边 { if(a[i].flag==0) continue; sum = 0; for(j = 0; j<=n; j++)//初始化 father[j] = j; sum = kruskal(i,m); if(sum == ans)//与之前的最小生成树比较,如果相等,那么肯定不是唯一的 { flag = 1; break; } } if(flag) printf("Not Unique!\n"); else printf("%d\n",ans); } return 0;}
0 0
- POJ1679:The Unique MST(次小生成树)
- POJ1679--The Unique MST(次小生成树)
- POJ1679 The Unique MST 【次小生成树】
- poj1679 The Unique MST(次小生成树)
- poj1679---The Unique MST(次小生成树)
- POJ1679 The Unique MST【Kruskal】【次小生成树】
- POJ1679 The Unique MST 次小生成树
- poj1679 The Unique MST(判定次小生成树)
- POJ1679 The Unique MST 次小生成树
- poj1679 The Unique MST(次小生成树)
- POJ1679 The Unique MST(次小生成树)
- The Unique MST poj1679 次小生成树
- POJ1679 The Unique MST次小生成树
- poj1679 The Unique MST(次小生成树)
- POJ1679 The Unique MST —— 次小生成树
- poj1679—The Unique MST(次小生成树)
- poj1679 The Unique MST 次小生成树
- 次小生成树的学习 POJ1679 The Unique MST
- 网站分析基础(二)网站流量来源简介
- div+css 学习笔记1
- SpringMVC集成Tiles和Jsp
- 6410 移植busybox+X+matchbox+gtk笔记
- 贪吃蛇----会动的
- POJ1679:The Unique MST(次小生成树)
- 四大会计师事务所之一的安永(Ernst & Young)称比特币不应该成为一种货币
- 053-707
- 基于HTML5技术的电力3D监控应用(一)
- SpringMVC集成Jsp,Freemarker,Velocity
- 使用crypto++ base64Decoder
- SQL查询为空时的结果转换
- cocos2dx之CCScaleBy,CCScaleTo
- NYOJ 40 公约数和公倍数