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. Size Balanced Tree(节点大小平衡树)

    定义 SBT也是一种自平衡二叉查找树,它的平衡原理是每棵树的大小不小于其兄弟树的子树的大小 即size(x->l)$\ge$size(x->r->l),size(x->r-&g ...

  2. 获取MMSQL数据库表信息

    SELECT 表名 then d.name else '' end, 表说明 then isnull(f.value,'') else '' end, 字段序号=a.colorder, 字段名=a.n ...

  3. 读书笔记--Struts 2 in Action 目录

    1.Struts 2:现代Web框架 1.1 web应用程序:快速学习 21.1.1 构建web应用程序 21.1.2 基础技术简介 31.1.3 深入研究 61.2 web应用程序框架 71.2.1 ...

  4. linux-jdk-mysql-tomcat安装

    1.JDK安装 注意:rpm与软件相关命令 相当于window下的软件助手 管理软件 步骤: 1)查看当前Linux系统是否已经安装java 输入 rpm -qa | grep java 1)卸载两个 ...

  5. centos下彻底删除mysql

    打算重新试试安装两个mysql,就把老的删除了. yum remove mysql mysql-server mysql-libs compat-mysql51 rm -rf /var/lib/mys ...

  6. history-之前发生了什么

    查看一下之前服务器上执行过的命令.看一下总是没错的,加上前面看的谁登录过的信息,应该有点用.另外作为admin要注意,不要利用自己的权限去侵犯别人的隐私哦. 到这里先提醒一下,等会你可能会需要更新 H ...

  7. HDU 4280 Island Transport(dinic+当前弧优化)

    Island Transport Description In the vast waters far far away, there are many islands. People are liv ...

  8. 洛谷P2381 圆圆舞蹈

    P2381 圆圆舞蹈 题目描述 熊大妈的乃修在时针的带领下,围成了一个圆圈舞蹈,由于没有严格的教育,奶牛们之间的间隔不一致. 奶牛想知道两只最远的奶牛到底隔了多远.奶牛A到B的距离为A顺时针走和逆时针 ...

  9. Leetcode661.Image Smoother图片平滑器

    包含整数的二维矩阵 M 表示一个图片的灰度.你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个,则尽可能多 ...

  10. sending data mysql slow Mysql查询非常慢的可能原因

    1.用explain看看mysql的执行情况,可以得知,task_id扫描了近20万条数据,而且这个task_id不是索引 2.为这个task_id所在的表,将此字段添加索引后,查询就变得很快了