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. SQL SERVER 2012 OBJECT_ID

    原来一个存储过程执行正常,升级sqlserver后提示临时表已存在,后查找资料 sql server 2012  OBJECT_ID('临时表')返回的数值是负数,在 2008r2及前是正数,所以导致 ...

  2. (递归)P1025 数的划分

    题解: #include<iostream>using namespace std;int ret=0,m_n;void p(int n,double k,int j){ if(k==1) ...

  3. 程序员:java中直接或间接创建线程的方法总结

    在java开发中,经常会涉及多线程的编码,那么通过直接或间接创建线程的方法有哪些?现整理如下: 1.继承Thread类,重写run()方法 class Worker extends Thread { ...

  4. POJ 1502:MPI Maelstrom Dijkstra模板题

    MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6499   Accepted: 4036 Des ...

  5. 大数据高可用集群环境安装与配置(03)——设置SSH免密登录

    Hadoop的NameNode需要启动集群中所有机器的Hadoop守护进程,这个过程需要通过SSH登录来实现 Hadoop并没有提供SSH输入密码登录的形式,因此,为了能够顺利登录每台机器,需要将所有 ...

  6. JS-表单非空验证

    JavaScript 表单验证 JavaScript 可用来在数据被送往服务器前对 HTML 表单中的这些输入数据进行验证. 实例:1.用户名的非空验证代码如下: <head> <m ...

  7. 对自己有用的VS调试技巧

    设置下一条语句 编辑然后继续 符号越界后查看堆对象 查看数组的值 底部 设置下一条语句 返回顶部 一个典型的调试情况就是通过单步跟踪分析为什么一个函数调用失败了.当你发现一个函数调用的另一个函数返回错 ...

  8. .NET 软件下面win10自动启动配置

    1.设置所有用户登录都能启动,打开文件夹 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp 2.给要启动的应用程序创建快捷方式, ...

  9. UML-用例关联

    1.用例关联:就是各个用例之间的关系,分3种关系分别是:包含关系.扩展关系.泛化关系. 2.包含关系 1).示例 2).使用场景 A.用例在其他用例中重复使用 B.用例非常复杂冗长,将其分解为子单元便 ...

  10. 网鼎杯-Fakebook-反序列化和SSRF和file协议读取文件

    0x00知识点:SSRF SSRF (Server-side Request Forge, 服务端请求伪造) 是一种由攻击者构造形成由服务端发起请求的一个安全漏洞.一般情况下,SSRF攻击的目标是从外 ...