The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions:34617   Accepted: 12637

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! 题意:求是否有第二个最小生成树
(次小生成树模板)
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<string>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull; const int maxn = + ;
const int maxv = + ;
const int max_dis = 1e9 + ; int T;
int n,m;
int a,b,c;
int MST,_MST;
bool vis[maxn];
int father[maxn];
int dist[maxn];
int graph[maxn][maxn];
bool used[maxn][maxn];
int max_edge[maxn][maxn]; void init() {
memset(vis,false,sizeof(vis));
memset(used,false,sizeof(used));
memset(max_edge,-,sizeof(max_edge));
memset(graph,0x7f,sizeof(graph));
} void input() {
scanf("%d%d",&n,&m);
for(int i=; i<m; i++) {
scanf("%d%d%d",&a,&b,&c);
graph[a][b]=graph[b][a]=c;
used[a][b]=used[b][a]=true;
}
} int prim() {
int ans=;
dist[]=;
vis[]=true;
father[]=-;
for(int i=; i<=n; i++) {
father[i]=;
dist[i]=graph[][i];
}
for(int i=; i<n; i++) {
int v=-;
for(int j=; j<=n; j++) {
if(!vis[j]&&(v==-||dist[j]<dist[v])) v=j;
}
ans+=dist[v];
vis[v]=true;
used[father[v]][v]=used[v][father[v]]=false;
for(int j=; j<=n; j++) {
if(vis[j]) {
max_edge[v][j]=max_edge[j][v]=max(max_edge[father[v]][j],dist[v]);
} else {
if(graph[v][j]<dist[j]) {
dist[j]=graph[v][j];
father[j]=v;
}
}
}
}
return ans;
} int second_prim() {
int ans=max_dis;
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
if(used[i][j]) ans=min(ans,MST+graph[i][j]-max_edge[i][j]);
return ans;
}
void solve() {
MST=prim();
_MST=second_prim();
if(MST==_MST) printf("Not Unique!\n");
else printf("%d\n",MST);
}
int main() {
scanf("%d",&T);
while(T--) {
init();
input();
solve();
}
return ;
}

最新文章

  1. YY一下微信线下支付的场景
  2. 用SysTick做的延时计时器
  3. win10 自动亮度关闭无效问题
  4. 《BI那点儿事》数据挖掘各类算法——准确性验证
  5. 12 Tips for Accurate Project Estimating
  6. Unity-Animator深入系列---fullPathHash和shortNameHash
  7. oracle 清除当前用户的回收站
  8. Android之按钮
  9. Javascript之return
  10. centos6.5用tomcat安装jenkins
  11. VC++中经常出现的内存泄露和指针问题
  12. iOS之UIWebView无法获取web标题
  13. 2017java文件操作(读写操作)
  14. [Codeforces]605E Intergalaxy Trips
  15. yii2 阿里云短信 aliyun-dysms
  16. 树莓派3 之 安装Mysql服务
  17. HashMap与TreeMap按照key和value排序
  18. Composer的学习
  19. Debug版本正常运行,Release版本编译通过但运行崩溃
  20. Why do we make statistics so hard for our students?

热门文章

  1. VMware运行时“内部错误”的解决方法
  2. Linux文件系统与目录结构
  3. 10.1.2 Document类型【JavaScript高级程序设计第三版】
  4. PHP处理mysql事务
  5. python3 练习题100例 (十九)
  6. poj 2965 枚举+DFS
  7. perl连接mysql数据库
  8. 设置默认以管理员运行的WinForm
  9. Keil如何生成bin文件【Keil生成Bin文件的方法】
  10. c语言的左移、右移