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. Day 2:线程与进程系列问题(二)

    补充: 线程的创建方式二: 1.自定义一个实现Runnable接口的类 2.实现Runnable接口中的run方法把自定义线程的任务写在run方法中 3.创建实现Runnable接口的对象 4.创建T ...

  2. Neo4j--windows下安装neo4j

    参考 https://www.cnblogs.com/ljhdo/archive/2017/05/19/5521577.html 安装包下载 https://neo4j.com/download-ce ...

  3. Java编译器 & Java解释器 & JVM

    转自:https://www.cnblogs.com/chengdabelief/p/6576320.html JVM JVM有自己完善的硬件架构,如处理器.堆栈(Stack).寄存器等,还具有相应的 ...

  4. Q4:Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays 官方的链接:4. Median of Two Sorted Arrays Description : There are two sort ...

  5. idea 2019.3 最新破解补丁和激活码,可破解至2089年!

    链接:https://blog.csdn.net/qq_42914528/article/details/85617901 上面方法失效了,请尝试以下方式激活(2020.1.6更新) idea激活码( ...

  6. Python说文解字_Python之多任务_03

    问:线程学完了,现在我们开始学习进程了吧? 答:是的.前面说到线程就是我们的手,我们现在可以学习一下我们的“胳膊”了. 我们有了多线程,为什么还要学习多进程呢?这是因为在Python当中有一把GIL锁 ...

  7. jquery---利用jquery插件生成二维码

    <script type="text/javascript" src="<?php echo RESOURCE_SITE_URL;?>/js/jquer ...

  8. Emoji表情符号兼容方案

    Emoji表情符号兼容方案 一 什么是Emoji    emoji就是表情符号:词义来自日语(えもじ,e-moji,moji在日语中的含义是字符) 表情符号现已普遍应用于手机短信和网络聊天软件. em ...

  9. 寒假day14

    今天去医院看脸了,回来继续写论文.

  10. java字符串排序(数字,字母,汉字等组合排序)

    package cn.cnnho.backstage.utils; import java.util.ArrayList;import java.util.Arrays;import java.uti ...