POJ 1679 The Unique MST

独特最小生成树与图论问题解决
本文探讨了在给定连通无向图的情况下,如何判断其唯一最小生成树的存在性。通过实例和算法解析,展示了如何利用克鲁斯卡尔算法来寻找最小生成树,并通过比较不同路径的成本来确定是否唯一。
The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 15153 Accepted: 5241

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

//这么久没刷题了、回到学校了、第一次写次小生成树,开心的是1A,不过写代码速度下降了
//I am back
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #define N 102 using namespace std; struct node { int a,b,w; bool visit; bool operator <(const node &t)const { return w<t.w; } }; struct Link { int to; int next; }; Link link[N]; int len[N][N]; int fa[N]; node Ed[5055]; int n,m,Min; int Find(int x) { if(x!=fa[x]) { return fa[x]=Find(fa[x]); } return x; } bool merge(int x,int y,int i) { x=Find(x); y=Find(y); if(x!=y) { int u,v; for(u=link[x].to;;u=link[u].next) { for(v=link[y].to;;v=link[v].next) { len[u][v]=len[v][u]=Ed[i].w; if(link[v].next==-1) break; } if(link[u].next==-1) break; } Ed[i].visit=1; link[u].next=y; fa[y]=x; return true; } return false; } void Kruskal() { int i; for(Min=i=0;i<m;i++) if(merge(Ed[i].a,Ed[i].b,i)) Min+=Ed[i].w; } int main() { int t,i; scanf("%d",&t); while(t--) { scanf("%d %d",&n,&m); for(i=1;i<=n;i++) fa[i]=i,link[i].to=i,link[i].next=-1; for(i=0;i<m;i++) { scanf("%d %d %d",&Ed[i].a,&Ed[i].b,&Ed[i].w); Ed[i].visit=0; } sort(Ed,Ed+m); Kruskal(); int Mi=-1,tp; for(i=0;i<m;i++) if(!Ed[i].visit) { tp=Min+Ed[i].w-len[Ed[i].a][Ed[i].b]; if(tp>Min) Mi=tp; else { Mi=tp;break; } } if(Min==Mi) printf("Not Unique!\n"); else printf("%d\n",Min); } return 0; }

转载于:https://www.cnblogs.com/372465774y/archive/2012/08/27/2658098.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值