The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 27141   Accepted: 9712

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!
【分析】最小生成树的唯一性,思路是先判断每条边是否有重边,有的话eq=1,否则0.然后第一次求出最小生成树,将结果记录下来,
然后依次去掉第一次使用过的且含有重边的边,再求一次最小生成树,若结果与第一次结果一样,则不唯一。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
const int M=;
int n,m,cnt;
int parent[N];
bool flag;
struct man {
int u,v,w;
int eq,used,del;
} edg[N];
bool cmp(man g,man h) {
return g.w<h.w;
}
void init() {
for(int i=; i<=; i++) {
parent[i]=i;
}
}
int Find(int x) {
if(parent[x] != x) parent[x] = Find(parent[x]);
return parent[x];
}//查找并返回节点x所属集合的根节点
void Union(int x,int y) {
x = Find(x);
y = Find(y);
if(x == y) return;
parent[y] = x;
}//将两个不同集合的元素进行合并
int Kruskal() {
init();
int sum=;
int num=;
for(int i=;i<m;i++){
if(edg[i].del==)continue;
int u=edg[i].u;int v=edg[i].v;int w=edg[i].w; if(Find(u)!=Find(v)){
sum+=w;
if(!flag)edg[i].used=;
num++;
Union(u,v);
}
if(num>=n-)break;
}
return sum;
}
int main() {
int t,d;
cin>>t;
while(t--) {
cnt=;
cin>>n>>m;
for(int i=; i<m; i++) {
cin>>edg[i].u>>edg[i].v>>edg[i].w;
edg[i].del=;
edg[i].used=;
edg[i].eq=;//一开始这个地方eq没有初始化,WA了好几发,操
}
for(int i=;i<m;i++){
for(int j=;j<m;j++){
if(i==j)continue;
if(edg[i].w==edg[j].w)edg[i].eq=;
}
}
sort(edg,edg+m,cmp);
flag=false;
cnt=Kruskal();
flag=true;
bool gg=false;
for(int i=;i<m;i++){
if(edg[i].used==&&edg[i].eq==){
edg[i].del=;
int s=Kruskal();//printf("%d %d\n",i,s);
if(s==cnt){
gg=true;
printf("Not Unique!\n");
break;
}
edg[i].del=;
}
}
if(!gg)cout<<cnt<<endl;
}
return ;
}

最新文章

  1. 有意思的Console
  2. python-切片 迭代 生成器
  3. Apache AB 如何传递参数
  4. Hex string convert to Binary String and Vise-Versa(16进制和2进制字符串的相互转换)
  5. php 小试 mysql-zmq-plugin 和 pthreads
  6. 模块加载----Webpack
  7. Echarts 地图(map)插件之 鼠标HOVER和tooltip自定义提示框
  8. java后台进程和线程优先级
  9. 将HTML特殊转义为实体字符的两种实现方式
  10. Java基础(41):Java中集合中需要注意的几个要点(关于Collection与Map)
  11. centos 7 systemctl
  12. tr设置背景图片
  13. shader 的 nounroll
  14. “前”方有坑,绕道而行(一)-- H5+CSS
  15. cve-2017-0199&amp;metasploit复现过程
  16. SqlBulkCopy 批量insert
  17. Java 删除ArrayList中重复元素,保持顺序
  18. 一个MySQL视图的优化过程
  19. zabbix_agent安装
  20. 异常 java.net.ConnectException: Connection refused: no further information

热门文章

  1. Visio中的Undo和Redo
  2. GDI绘图中的映射模式CDC::SetMapMode()
  3. JQuery拖拽改变元素的尺寸
  4. javaScript中的this关键字解析
  5. 动态规划:状压DP-斯坦纳树
  6. 【STSRM13】木之本樱
  7. react框架
  8. HBase表操作
  9. 利用os、hash模块生成目录下所有文件的md5
  10. MFC/Socket网络编程