POJ1679The Unique MST(次小生成树)

时间:2023-02-21 22:16:33
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&lpar;次小生成树模板&rpar;

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

  2. POJ1679 The Unique MST&lbrack;次小生成树&rsqb;

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

  3. POJ 1679 The Unique MST &lpar;次小生成树 判断最小生成树是否唯一&rpar;

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

  4. POJ&lowbar;1679&lowbar;The Unique MST&lpar;次小生成树&rpar;

    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&comma;次小生成树模板题

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

  7. POJ&lowbar;1679&lowbar;The Unique MST&lpar;次小生成树模板&rpar;

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

  8. POJ 1679 The Unique MST &lpar;次小生成树&rpar;

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

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

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

  10. poj 1679 The Unique MST &lpar;次小生成树&lpar;sec&lowbar;mst&rpar;【kruskal】&rpar;

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

随机推荐

  1. setView的AlertDialog在受到二次点击后出错

    错误报告: 10-21 13:11:16.009: E/AndroidRuntime(27937): FATAL EXCEPTION: main10-21 13:11:16.009: E/Androi ...

  2. 如何让用户只能访问特定的数据库&lpar;MSSQL&rpar;

    背景 客户的SQL Server实例上有多个厂商的数据库,每个数据库由各自的进行厂进行商维护, 为了限定不同厂商的维护人员只能访问自己的数据库,现需要给各个厂商限定权限,让他们登录SQL Server ...

  3. Java 基础【13】 文件&lpar;文件夹&rpar; 创建和删除

    使用 java.io.file 创建文件(文件夹),算是 java 最基础的知识,但实战项目中还是需要知晓细节. 比如 File 类中的 mkdir() 和 mkdirs() 的区别. JDK API ...

  4. selenium提供了三种模式的断言:assert&comma;verify&comma;waitfor

    Assert:失败时,该测试将终止 Verify:失败时,该测试继续执行,并将错误日志记录在日显示屏 Waitfor:等待某些条件变为真,一般使用在AJAX应用程序的测试 断言常用的有,具体见如下:a ...

  5. GeoHash原理解析

    GeoHash 核心原理解析       引子 一提到索引,大家脑子里马上浮现出B树索引,因为大量的数据库(如MySQL.oracle.PostgreSQL等)都在使用B树.B树索引本质上是对索引字段 ...

  6. JSON文本转换为JSONArray 转换为 List&lt&semi;Object&gt&semi;

    package com.beijxing.TestMain; import java.io.File; import java.io.IOException; import java.util.Arr ...

  7. phpredis中文手册——《redis中文手册》 php版

    本文是参考<redis中文手册>,将示例代码用php来实现,注意php-redis与redis_cli的区别(主要是返回值类型和参数用法). 目录(使用CTRL+F快速查找命令): Key ...

  8. HL7及PIX相关的测试工具

    最近在开发PIX项目时需要一些工具, 比如PIX各个Actor的测试工具, HL7消息的验证工具等等. 下面列下我找见的几个 必备工具. 1. http://hit-testing.nist.gov: ...

  9. Android Toast 提示按两次返回键退出

    public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceS ...

  10. iOS 指南针的制作 附带源码

    iOS  指南针的制作  附带源码 代码下载地址: http://vdisk.weibo.com/s/HK4yE   http://pan.baidu.com/share/link?shareid=7 ...