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. Luogu P4011 孤岛营救问题(状态压缩+最短路)

    P4011 孤岛营救问题 题意 题目描述 \(1944\)年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛,营救被敌军俘虏的大兵瑞恩.瑞恩被关押在一个迷宫里,迷宫地形复杂,但幸好麦克得到 ...

  2. MySQL系列(六)--索引优化

    在进行数据库查询的时候,索引是非常重要的,当然前提是达到一定的数据量.索引就像字典一样,通过偏旁部首来快速定位,而不是一页页 的慢慢找. 索引依赖存储引擎层实现,所以支持的索引类型和存储引擎相关,同一 ...

  3. linux下C++遍历文件夹下的全部文件;Windows/Linux下C++批量修改文件名,批量删除文件

    Linux下 C++遍历目录下所有文件 rename(image_path.c_str(), image_path_new.c_str()); remove(image_path_move.c_str ...

  4. Django项目:CRM(客户关系管理系统)--34--26PerfectCRM实现King_admin自定义排序

    ordering = ['-qq'] #自定义排序,默认'-id' #base_admin.py # ————————24PerfectCRM实现King_admin自定义操作数据———————— f ...

  5. bzoj 4241 历史研究——分块(区间加权众数)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4241 套路:可以大力预处理,如果求区间加权众数,可以预处理i~j块(或 j 位置)的最大值, ...

  6. 20190829 [ Night ] - 玻

    又考试 半集训真累…… 老帅哥露出会心的微笑.jpeg +------------+ | 寸 | | 土 分 | | 不 分 | | 让 必 | | 争 | +------------+ 考试过程: ...

  7. SpringBoot集成lombook让代码更简洁

    1)添加lombok依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>l ...

  8. WebGis二次开发包实例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs& ...

  9. WebBrowser修改默认白色背景

      背景:在使用Winform的WebBrowser显示网页的时候,在网页还未加载完成之前会显示白色,刚好网页内容呈现黑色,这样视觉效果上就十分差,想把这层白色的去掉. 试了很久之后发现根本去不掉,应 ...

  10. Spring Boot邮件功能

    1.应用场景 邮件功能的应用场景可谓十分广泛,诸如注册用户.密码找回,消息通知.以及一些程序异常通知等都需要使用到该功能. 正是由于邮件功能的使用广泛,因此springboot也加在它的组件中添加了邮 ...