The Unique MST

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22668   Accepted: 8038

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!

【次小生成树】:

重要的是理解求次小生成树的过程。求次小生成树建立在Prim算法的基础上。可以确定的是,次小生成树肯定是由最小生成树删去一条边再加上一条边得到。那么我们应该删去哪条边再加上哪条边呢?假设两点u,v之间有一条边且这条边不在MST中,那么可以尝试加上这条边。但是加上这条边以后会出现环,则一定要去掉回路上的一条边,这条边应该选择回路上权值最大的那条边(毕竟权值要求尽量小)。尝试对每对点对进行上述操作,那么最小的那个结果就是次小生成树。

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxm = 1e6 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
int dir[][] = {{,},{,-},{-,},{,}};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
const int mod = ;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn = ; int u,v,w;
int n,m,ans,k,sum,cnt;
struct node
{
int u,v,w;
}e[maxn];
int fa[maxn];
int mst[maxn];
int Find(int x)
{
if(fa[x]!=x)
fa[x]=Find(fa[x]);
return fa[x];
}
void join(int x,int y)
{
int xx = Find(x);
int yy = Find(y);
fa[xx]=yy;
}
bool cmp(node a,node b)
{
return a.w<b.w;
}
void kruskal()
{
rep(i,,n-) //枚举mst内的n-1个点
{
cnt=,sum=;
rep(j,,n) fa[j]=j; //每次枚举待删边 重新设置
rep(j,,m)
{
if(j != mst[i]) //枚举不在mst中的每一条边
if(Find(e[j].u) != Find(e[j].v))
{
join(Find(e[j].u), Find(e[j].v));
sum += e[j].w;
cnt++;
}
}
if(cnt==n- && sum==ans) //最小=次小——>不唯一
{
printf("Not Unique!\n");
return ;
}
}
printf("%d\n",ans);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
sum=,k=,ans=;
scanf("%d %d",&n, &m);
for(int i=;i<=n;i++)
fa[i]=i;
for(int i=; i<=m; i++)
scanf("%d %d %d",&e[i].u, &e[i].v, &e[i].w);
sort(e+,e+m+,cmp);
for(int i=;i<=m;i++) //kruskal造mst数组存储其中包含的边的编号
{
int x=Find(e[i].u);
int y=Find(e[i].v);
if(x != y)
{
join(x,y);
ans+=e[i].w;
mst[++k]=i; //先生成mst,存储mst的每一条边
}
}
kruskal(); //然后枚举删除每一条mst中的边,看是否能找出一课次小生成树的权值和最小生成树的权值相等。
//如果相等,则不唯一
}
}
/*
【题意】
给你n个点m条边的图,判断图的最小生成树是否唯一。 【类型】
次小生成树 【分析】
首先求出最小生成树的结果sum,并记录每条边是否属于最小生成树,如果存在和sum相同的生成树,则此生成树一定包含不属于最小生成树的边,
即枚举每条不属于最小生成树的边,对每条边求最小生成树,如果结果和sum相同,则not unique,否则最小生成树唯一,输出sum. 【时间复杂度&&优化】
16ms 【trick】
*/

POJ 1679 The Unique MST 【最小生成树/次小生成树模板】的更多相关文章

  1. poj 1679 The Unique MST (次小生成树模板题)

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

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

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

  3. poj 1679 The Unique MST 【次小生成树+100的小数据量】

    题目地址:http://poj.org/problem?id=1679 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 Outpu ...

  4. POJ 1679 The Unique MST:次小生成树【倍增】

    题目链接:http://poj.org/problem?id=1679 题意: 给你一个图,问你这个图的最小生成树是否唯一. 题解: 求这个图的最小生成树和次小生成树.如果相等,则说明不唯一. 次小生 ...

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

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

  6. POJ 1679 The Unique MST (次小生成树)题解

    题意:构成MST是否唯一 思路: 问最小生成树是否唯一.我们可以先用Prim找到一棵最小生成树,然后保存好MST中任意两个点i到j的这条路径中的最大边的权值Max[i][j],如果我们能找到一条边满足 ...

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

    题意:求解最小生成树的权值是否唯一,即要我们求次小生成树的权值两种方法求最小生成树,一种用prim算法, 一种用kruskal算法 一:用prim算法 对于给定的图,我们可以证明,次小生成树可以由最小 ...

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

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

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

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

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

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

随机推荐

  1. [LeetCode] Simplify Path,文件路径简化,用栈来做

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  2. 本地更新代码同步至github仓库

    昨晚在家里写了一个demo放到github上,然后今天晚上来公司准备搞一下,但是git pull下来在本地修改之后push不到github上,然后发现公司电脑上并没有access权限,然后想起来还没配 ...

  3. K-近邻(KNN)算法

    1,KNN算法对未知类别属性的数据集中的每个点依次执行以下操作: 计算已知类别数据集中的点与当前点之间的距离; 按照距离递增排序; 选取与当前点距离最小的k个点; 确定前k个点所在类别的出现频率; 返 ...

  4. 基于FPGA的HDTV视频图像灰度直方图统计算法设计

    随着HDTV的普及,以LCD-TV为主的高清数字电视逐渐进入蓬勃发展时期.与传统CRT电视不同的是,这些高清数字电视需要较复杂的视频处理电路来驱动,比如:模数转换(A/D Converter).去隔行 ...

  5. MySQL 表和库删不掉,并且表也打不开,不能导出的情况

    linux上的mysql中,最近遇到表和库删不掉,并且表也打不开,不能导出的情况. 在删除数据库时,出现以下错误: ERROR 1010 (HY000): Error dropping databas ...

  6. 24、redis中的sentinel的作用?

    redis中的sentinel的作用? Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Re ...

  7. Microsoft Visual Studio TFS 切换登录用户的方法

    方法一 用VS2012开发项目,一直困扰着我的是不知道怎么去切换TFS的登录帐户,因为每次TFS都是用默认的账户连接到TFS服务器,有时想切换另一个用户登录TFS服务器, 不知道从何下手,今天总算找到 ...

  8. Chrome 浏览器 autocomplete off无效

    在表单填写时突然发现autocomplete 失效了 网上搜索后得出大概意思是在某些情况下确实无效[捂脸] 解决方案 大致原因是浏览器默认为type为password的input标签自动填充密码 这样 ...

  9. 运维开发:python websocket网页实时显示远程服务器日志信息

    功能:用websocket技术,在运维工具的浏览器上实时显示远程服务器上的日志信息 一般我们在运维工具部署环境的时候,需要实时展现部署过程中的信息,或者在浏览器中实时显示程序日志给开发人员看.你还在用 ...

  10. 几个例子理解对称加密与非对称加密、公钥与私钥、签名与验签、数字证书、HTTPS加密方式

    # 原创,转载请留言联系 为什么会出现这么多加密啊,公钥私钥啊,签名啊这些东西呢?说到底还是保证双方通信的安全性与完整性.例如小明发一封表白邮件给小红,他总不希望给别人看见吧.而各种各样的技术就是为了 ...