The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 25203   Accepted: 8995

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! 题意:问一棵最小生成树是否唯一
找次小生成树,如果相等不唯一,否则唯一 次小生成树:就是最小生成树换一条边而成的生成树;用maxd【x】【y】存储最小生成树两个节点(x,y)路径中最大的那条边的权值,也就是最小生成树中x-z-...-y中那个最大的那一个权值,然后就可以换边了,到底换哪一个边就从不在生成树中的边一个一个枚举咯,假设x-y,如果要用x-y替换x-z...-y肯定得替换x-z...-y中权值最大的那条边才能让最后得到结果只比x-z...-y小一点,也就是仅次于
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAX = ;
const int INF = ;
int n,m,ans,cnt;
int edge[MAX][MAX],vis[MAX],used[MAX][MAX],dist[MAX];
int pre[MAX],maxd[MAX][MAX];
void prime()
{
memset(vis,,sizeof(vis));
memset(used,false,sizeof(used));
memset(maxd,,sizeof(maxd));
for(int i = ; i <= n; i++)
{
dist[i] = edge[][i];
pre[i] = ;
}
dist[] = ;
vis[] = ;
pre[] = -;
for(int i = ; i < n; i++)
{
int minn = INF,pos = -;
for(int j = ; j <= n; j++)
{
if(vis[j] == && dist[j] < minn)
{
minn = dist[j];
pos = j;
}
}
if(minn == INF)
{
ans = INF;
return;
}
ans += minn;
vis[pos] = ;
used[pos][pre[pos]] = used[ pre[pos] ][pos] = true;
for(int j = ; j <= n; j++)
{
if(vis[j])
maxd[j][pos] = maxd[pos][j] = max(dist[pos], maxd[j][pre[pos]]);
if(vis[j] == && dist[j] > edge[pos][j])
{
dist[j] = edge[pos][j];
pre[j] = pos;
}
}
}
}
void smst()
{
cnt = INF;
for(int i = ; i <= n; i++)
{
for(int j = i + ; j <= n; j++)
{
if(used[i][j] == false && edge[i][j] != INF)
{
cnt = min(cnt, ans - maxd[i][j] + edge[i][j]);
}
}
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
for(int i = ; i <= MAX; i++)
for(int j = ; j <= MAX; j++) //初始化的时候把MAX,写成了n, orz....
{
if(i != j)
edge[i][j] = INF;
else
edge[i][i] = ;
}
scanf("%d%d", &n,&m);
for(int i = ; i < m; i++)
{
int x,y,w;
scanf("%d%d%d", &x, &y, &w);
edge[x][y] = edge[y][x] = w;
}
ans = ;
prime();
smst();
if(ans == INF)
{
printf("Not Unique!\n");
continue;
}
if(cnt == ans)
{
printf("Not Unique!\n");
}
else
{
printf("%d\n",ans);
}
}
return ;
}
												

POJ1679The Unique MST(次小生成树)的更多相关文章

  1. poj1679The Unique MST(次小生成树模板)

    次小生成树模板,别忘了判定不存在最小生成树的情况 #include <iostream> #include <cstdio> #include <cstring> ...

  2. POJ1679 The Unique MST[次小生成树]

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28673   Accepted: 10239 ...

  3. POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)

    题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...

  4. POJ_1679_The Unique MST(次小生成树)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

  5. POJ1679 The Unique MST —— 次小生成树

    题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  6. POJ-1679 The Unique MST,次小生成树模板题

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K       Description Given a connected undirec ...

  7. POJ_1679_The Unique MST(次小生成树模板)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23942   Accepted: 8492 D ...

  8. POJ 1679 The Unique MST (次小生成树)

    题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...

  9. POJ 1679 The Unique MST (次小生成树kruskal算法)

    The Unique MST 时间限制: 10 Sec  内存限制: 128 MB提交: 25  解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...

  10. poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35999   Accepted: 13145 ...

随机推荐

  1. angularjs的ng-repeat指令下的scope作用域

    ng-repeat指令在迭代的时候,每次迭代都会创建一个新的scope,比如下面的代码: <div ng-repeat="list in lists" ng-controll ...

  2. 分布式监控系统Zabbix-3.0.3-完整安装记录(6)-微信报警部署

    Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式. 现在由于微信使用的广泛度,越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信息推 ...

  3. Nginx反向代理+负载均衡简单实现(http方式)

    1)nginx的反向代理:proxy_pass2)nginx的负载均衡:upstream 下面是nginx的反向代理和负载均衡的实例: 负载机:A机器:103.110.186.8/192.168.1. ...

  4. swift为UIView添加extension扩展frame

    添加swift file:UIView+Extension import UIKit extension UIView { // x var x : CGFloat { get { return fr ...

  5. WinForm 快捷键设置

    一.窗体快捷键,只在窗体上有效果 首先在form_load的时候写上this.KeyPreview=true;//表示窗体接受按键事件 然后如下 private void Frm_KeyDown(ob ...

  6. 【Android性能优化】(一)使用SparseIntArray替换HashMap

    SparseArray是android里为<Interger,Object>这样的Hashmap而专门写的class,目的是提高效率,其核心是折半查找函数(binarySearch)

  7. JNI 程序开发

    参考资料: http://blog.csdn.net/wwj_748/article/details/28136061 JNI_最简单的Java调用C/C++代码 http://blog.csdn.n ...

  8. pandas 修改 DataFrame 列名

    问题: 有一个DataFrame,列名为:['$a', '$b', '$c', '$d', '$e'] 现需要改为:['a', 'b', 'c', 'd', 'e'] 有何办法? import pan ...

  9. Gradle tip #3: Tasks ordering

    I noticed that the quite often problem I face when I work with Gradle - is tasks ordering (either ex ...

  10. HBase入库调优

    本文章只针对“微型集群处理大数据”的场景. 场景描述: 硬件:5个节点,每个节点可用硬盘1块(700G.500G等).8核cpu,实验室环境(有时候还要跑其他程序跟你抢占资源),16G内存. 软件:h ...