The Unique MST

Time Limit: 1000MS Memory Limit: 10000K

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 <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <map> using namespace std; const int maxn = 105;
const int INF = 1e9+7; int s[maxn][maxn],n,m; int prim()
{
int i,j,MIN,k,sum = 0;
int dis[maxn],f[maxn],pre[maxn],maxx[maxn][maxn];/*maxx用来存储最小生成树的 i 到 j 的最大边权值*/
int use[maxn][maxn];
for(i=1;i<=n;i++)
{
dis[i] = s[1][i];
pre[i] = 1;
f[i] = 0;
}
memset(maxx,0,sizeof(maxx));
memset(use,0,sizeof(use));
f[1] = 1;
for(i=1;i<n;i++)
{
MIN = INF;
k = -1;
for(j=1;j<=n;j++)
if(!f[j]&&MIN>dis[j])
{
MIN = dis[j];
k = j;
}
if(MIN==INF)
return -1;
//printf("MIN %d\n",MIN);
f[k] = 1;
sum += MIN;
for(j=1;j<=n;j++)
{
if(f[j])
maxx[k][j] = maxx[j][k] = max(maxx[pre[k]][j],dis[k]);/*(发现直接等于dis[k]也行)*/
else
{
if(dis[j]>s[k][j])
{
dis[j] = s[k][j];
pre[j] = k;
}
}
}
}
//printf("%d\n",sum);
MIN = INF;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(i!=pre[j]&&j!=pre[i]&&s[i][j]!=INF)
MIN = min(MIN,sum-maxx[i][j]+s[i][j]);
}
}
//printf("%d\n",MIN);
if(MIN==sum)
return -1;
else
return sum;
} int main()
{
int t,i,j,a,b,c;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
s[i][j] = i==j?0:INF;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(s[a][b]>c)
s[a][b] = s[b][a] = c;
}
a = prim();
if(a==-1)
printf("Not Unique!\n");
else
printf("%d\n",a);
}
return 0;
}

POJ - 1679_The Unique MST的更多相关文章

  1. POJ 1679The Unique MST

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

  2. poj 1679 The Unique MST

    题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...

  3. POJ 1679 The Unique MST (最小生成树)

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

  4. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  5. poj 1679 The Unique MST 【次小生成树】【模板】

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

  6. poj 1679 The Unique MST (判定最小生成树是否唯一)

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

  7. POJ 1679 The Unique MST (最小生成树)

    The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...

  8. poj 1679 The Unique MST【次小生成树】

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

  9. POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

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

随机推荐

  1. 解决导入TensorFlow后出现警告的的问题解决:通过降低numpy的版本

    在原有基础上安装tensorflow 重新虚拟出一个环境安装tensorflow 安装 测试 大多教程都是重新虚拟出一个环境,原有环境就可以支持为什么还要重建一个新的环境,如果以后遇到坑了更新解释. ...

  2. Opencv中RGB通道/HSV通道并分离

    OpenCV中HSV颜色模型及颜色分量范围 opencv HSV 颜色模型(H通道取值 && CV_BGR2HSV_FULL) [opencv]在hsv颜色空间识别区域颜色 将原图分离 ...

  3. H5C3--边框图片

    类似于android的.9图片,目的是为了防止图片因为内容的扩展而导致图片拉伸失真. <!DOCTYPE html> <html lang="en"> &l ...

  4. 使用Jedis操作Redis-使用Java语言在客户端操作---hash类型

        我们可以将Redis中的Hashes类型看成具有String Key和String Value的map容器.            所以该类型非常适合于存储值对象的信息.如Username.P ...

  5. spring springmvc 展示图片,静态资源的处理

    jsp中显示一张照片 <img alt="静态图片" src="static/目录.png"> 然后在springmvc的配置中加上 <!-- ...

  6. MySQL错误1055

    问题描述:在MySQL数据库下,执行SQL插入语句报错.错误信息如下: 错误原因:在MySQL5.7之后,sql_mode中默认存在ONLY_FULL_GROUP_BY,SQL语句未通过ONLY_FU ...

  7. ActiveMQ消息中间件

    最近学习到ActiveMQ,之前也没有用过相关或者类似的工具,因此特地写个文章进行相关的学习记录. 相关参考博文:https://www.cnblogs.com/cyfonly/p/6380860.h ...

  8. I Love Palindrome String

    I Love Palindrome String 时间限制: 2 Sec  内存限制: 128 MB 题目描述 You are given a string S=s1s2..s|S| containi ...

  9. 洛谷P1774 最接近神的人_NOI导刊2010提高(02) [2017年6月计划 线段树03]

    P1774 最接近神的人_NOI导刊2010提高(02) 题目描述 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某种活动的图案.而石门 ...

  10. mybatis添加数据返回主键

    程序结构图: 表结构: 创表sql: Create Table CREATE TABLE `users` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `us ...