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!
方法1:首先算出最小生成树的权值和ans,然后枚举删除最小生成树中的每一条边,若还可以达到相同的效果,就说明最小生成树不唯一,
因为两个不同的最小生成树至少有一条边不同,所以我们才可以枚举删除每一条边.
方法2:判断最小生成树和次小生成树的权值是否相同.
#include<iostream>
#include<vector>
#include<algorithm>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int maxn=;
int f[maxn];
struct node
{
int u,v,w;
bool operator < (const node &r)const{
return w<r.w;
}
}q[maxn];
int Find(int x)
{
return f[x]==x?x:f[x]=Find(f[x]);
}
int Merge(int u,int v)
{
u=Find(u);
v=Find(v);
if(u!=v)return f[u]=v,;
return ;
}
vector<int>v;
int main()
{
int T;
cin>>T;
while(T--){
v.clear();
int n,m;
cin>>n>>m;
for(int i=;i<=n;i++)f[i]=i;
for(int i=;i<=m;i++){
cin>>q[i].u>>q[i].v>>q[i].w;
}
sort(q+,q++m);
int ans=;
for(int i=;i<=m;i++){
int x=Merge(q[i].u,q[i].v);
if(x){
v.push_back(i);
ans+=q[i].w;
}
}
int flag=;
for(int i=;i<v.size();i++){
int sum=,cnt=;
for(int j=;j<=n;j++)f[j]=j;
for(int j=;j<=m;j++){
if(j==v[i])continue;
int x=Merge(q[j].u,q[j].v);
if(x){
sum+=q[j].w;
cnt++;
}
}
if(cnt==n-&&ans==sum){
flag=;
break;
}
}
if(flag)cout<<ans<<endl;
else printf("Not Unique!\n"); }
return ;
}
#include<iostream>
#include<cstring> using namespace std;
typedef long long ll;
const int maxn=;
const int INF=0x3f3f3f3f;
int Maxlen[maxn][maxn];
int dis[maxn],vis[maxn];
int pre[maxn],MAP[maxn][maxn];
int used[maxn][maxn];
int n,m; int Prim(int x)
{
memset(Maxlen,,sizeof(Maxlen));
memset(dis,INF,sizeof(dis));
memset(vis,,sizeof(vis));
memset(pre,,sizeof(pre));
memset(used,,sizeof(used));
for(int i=;i<=n;i++){
dis[i]=MAP[x][i];
pre[i]=x;
}
dis[x]=;
vis[x]=;
pre[x]=;
int ans=;
for(int i=;i<=n;i++){
int u=,minn=INF;
for(int j=;j<=n;j++){
if(!vis[j]&&dis[j]<minn){
u=j;
minn=dis[j];
}
}
vis[u]=;
ans+=minn;
used[u][pre[u]]=used[pre[u]][u]=;
for(int v=;v<=n;v++){
if(vis[v]){
Maxlen[u][v]=Maxlen[v][u]=max(Maxlen[v][pre[u]],dis[u]);
}
else{
if(dis[v]>MAP[u][v]){
dis[v]=MAP[u][v];
pre[v]=u;
}
}
}
}
return ans;
}
void sst(int ans)
{
int sum=INF;
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
if(!used[i][j]&&MAP[i][j]!=INF){
sum=min(sum,ans+MAP[i][j]-Maxlen[i][j]);
}
}
}
if(sum==ans)cout<<"Not Unique!"<<endl;
else cout<<ans<<endl;
}
int main()
{
int T;
cin>>T;
while(T--){
cin>>n>>m;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i==j)MAP[i][j]=;
else MAP[i][j]=INF;
}
}
for(int i=;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
MAP[u][v]=MAP[v][u]=min(MAP[u][v],w);
}
int ans=Prim();
sst(ans);
}
return ;
}

K - The Unique MST (最小生成树的唯一性)的更多相关文章

  1. The Unique MST(最小生成树的唯一性判断)

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

  2. K - The Unique MST

    K - The Unique MST #include<iostream> #include<cstdio> #include<cstring> #include& ...

  3. [poj1679]The Unique MST(最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28207   Accepted: 10073 ...

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

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

  5. K - The Unique MST - poj 1679

    题目的意思已经说明了一切,次小生成树... ****************************************************************************** ...

  6. POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)

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

  7. poj1679 The Unique MST(最小生成树唯一性)

    最小生成树的唯一性,部分参考了oi-wiki 如果一条不在最小生成树边集内的边,它可以替换一条在最小生成树边集内,且权值相等的边,那么最小生成树不是唯一的 同过kruskal来判断 考虑权值相等的边, ...

  8. (poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)

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

  9. The Unique MST (判断是否存在多个最小生成树)

    The Unique MST                                                                        Time Limit: 10 ...

随机推荐

  1. ODBC OLEDB

    ODBC  OLEDB https://www.cnblogs.com/dachuang/p/8615754.html

  2. ubuntu 18.04 安装 Redis-server

    Redis 安装 Redis是一款内存键值存储,以其灵活性,性能和广泛的语言支持而闻名.本教程将演示如何在Ubuntu 18.04服务器上安装和配置Redis.主要内容包括: 安装 Redis Red ...

  3. web页面内容打印总结

    web页面打印有两种,一种是直接调用window.print()命令操作,一种是使用ActiveX插件(Object标签)操作,但是第二种只支持IE内核的浏览器. 示例1: <!DOCTYPE ...

  4. 201709-1 打酱油 Java

    思路: 先看能不能买5瓶,因为送的最多,然后看能不能买3瓶,最后一瓶一瓶地买 import java.util.Scanner; public class Main { public static v ...

  5. 关于PHP索引数组unset某key后json_encode相关问题踩坑记录

    <?php $a = [1,2,3]; var_dump(json_encode($a)); #string(7) "[1,2,3]" unset($a[0]); var_d ...

  6. Python—程序设计:观察者模式

    观察者模式 内容:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新.观察者模式又称“发布-订阅”模式. 角色: 抽象主题(Subject) 具体 ...

  7. 详细的git入门级别,从安装到实战

    拥有自己码云开源网站,想要上传项目到码云怎么操作?公司新技术提升由Svn转为Git,慌不慌?想要从Github开源网站下载开源项目,难道还依赖直接下载项目然后解压导入项目工程?下面可以通过及其简易且好 ...

  8. sqlserver2008的sql语句支持的最大长度

    想写一个sql语句,很长,主要是in后跟着无数个用户ID,(虽然实现方式很低级,但是还是凑合着用吧) 不知道sql最大长度是多少,看了 SQL Server 的最大容量规范,写的是 包含 SQL 语句 ...

  9. 触发器-- 肖敏_入门系列_数据库进阶 60、触发器(三) --youku

    二 https://v.youku.com/v_show/id_XMzkxOTc5NDY0OA==.html?spm=a2h0k.11417342.soresults.dtitle 三 https:/ ...

  10. chrome安装switchyomega

    由于在国外网站找不到下载链接,在国内招了个crx文件,以下为安装crx教程 首先修改后缀为zip,再解压, 得到以下文件 然后在chrome里找到扩展程序, 打开开发者模式,点击-加载已解压的扩展程序 ...