The Unique MST
来源:互联网 发布:乐视tv网络电视好用吗 编辑:程序博客网 时间:2024/04/28 21:50
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!
题解:次小生成树,未优化的普利姆做法。每次把当前的最小生成树的任意两点之间的最大值保存,然后遍历没有用到的边,遍历i与j之间,如果i,j的权等于最小生成树上i->j的最大值,肯定存在第二个生成树,因为我可以删除原来那一条,用现在这一条顶替。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int INF = 0x3fffffff;int map[203][203];bool visited[203];int d[203];int w[203][203]; //生成树上两点之间的最大值 bool s[203][203]; //记录生成树的边 int pre[203];int ans;int max(int a,int b){return a > b ? a : b;}int min(int a,int b){return a > b ? b : a;}bool prim(int n){memset(visited,false,sizeof(visited));memset(w,0,sizeof(w));memset(s,false,sizeof(s));for(int i = 1;i <= n;i++){d[i] = map[1][i];w[1][i] = w[i][1] = map[1][i]; pre[i] = 1;}visited[1] = true;ans = 0;for(int i = 1;i < n;i++){int min = INF;int k;for(int j = 1;j <= n;j++){if(!visited[j] && min > d[j]){min = d[j];k = j;}}s[pre[k]][k] = s[k][pre[k]] = true;if(min == -1){return false;}ans += min;visited[k] = true;for(int j = 1;j <= n;j++){if(visited[j]) //更新j到其他点的最大值,其他访问了的点和该点的生成树路径上的边已经得到 {w[j][k] = w[k][j] = max(w[j][pre[k]],map[pre[k]][k]);}if(!visited[j] && d[j] > map[k][j]){d[j] = map[k][j];pre[j] = k;}}}return true;}int main(){int n,m;int ncase;cin>>ncase;while(ncase--){scanf("%d%d",&n,&m);for(int i = 1;i <= n;i++){for(int j = 1;j <= n;j++){if(i == j){map[i][j] = 0;}else{map[i][j] = INF;}}}int u,v,c;for(int i = 0;i < m;i++){scanf("%d%d%d",&u,&v,&c);map[u][v] = map[v][u] = min(map[u][v],c);}prim(n);bool flag = true;for(int i = 1;i <= n;i++){for(int j = 1;j <= n;j++){if(map[i][j] != INF && !s[i][j] && i != j){if(w[i][j] == map[i][j]) //i-j的边(没有在生成树上)等于生成树上i->j路径上的最大值 {flag = false;break;}}}}if(flag){printf("%d\n",ans);}else{printf("Not Unique!\n");}}return 0;}
0 0
- The Unique MST
- pku1679 The Unique MST
- PKU1679 the unique mst
- POJ1679:The Unique MST
- The Unique MST
- The Unique MST(P1679)
- poj_1679 The Unique MST
- POJ1679 The Unique MST
- 【hdu】 The Unique MST
- poj1679 - The Unique MST
- POJ1679 The Unique MST
- poj The Unique MST
- poj1679 The Unique MST
- The Unique MST
- poj1679 The Unique MST
- The Unique MST
- The Unique MST--hdoj
- The Unique MST
- linux fork函数与vfork函数
- Ubuntu+nginx+uwsgi部署Django项目
- 黑马程序员---学习笔记7:指针
- android:maxHeight,android:maxWidth失效
- JAVA字符串格式化-String.format()的使用
- The Unique MST
- 黑马程序员---学习笔记8:数组指针
- android 中scrollView顶部图片下拉放大
- ListView+CheckBox(二)
- git提示please tell me who you are
- 黑马程序员---学习笔记9:构造类型和结构体
- Could not open a connection to your authentication agent
- 数学分析告诉偶们什么(vamei)
- 黑马程序员---学习笔记10:结构体数组