The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20737   Accepted: 7281

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"stdio.h"
#include"string.h"
#define inf 100000000
#define M 1111
int G[M][M],maxd[M][M],use[M],dis[M],pre[M],vis[M][M];
int max(int a,int b)
{
return a>b?a:b;
}
int min(int a,int b)
{
return a<b?a:b;
}
int dij(int u,int n)
{
int ans=0,i,j;
memset(use,0,sizeof(use));
memset(maxd,0,sizeof(maxd));//记录不在任意两点在在生成树的路径中的最长边
memset(vis,0,sizeof(vis));//标记边是否在生成树里面
for(i=1;i<=n;i++)
{
dis[i]=G[u][i];
pre[i]=u;//记录父节点
}
dis[u]=0;
use[u]=1;
for(i=1;i<n;i++)
{
int mini=inf;
int tep=-1;
for(j=1;j<=n;j++)
{
if(!use[j]&&dis[j]<mini)
{
mini=dis[j];
tep=j;
}
}
if(tep==-1)break;
use[tep]=1;
vis[tep][pre[tep]]=vis[pre[tep]][tep]=1;
ans+=mini;
for(j=1;j<=n;j++)
{
if(!use[j]&&dis[j]>G[tep][j])
{
dis[j]=G[tep][j];
pre[j]=tep;
}
if(j!=tep)
maxd[tep][j]=maxd[j][tep]=max(mini,maxd[pre[tep]][j]);//更新
}
}
return ans;
}
int main()
{
int T,m,n,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
G[i][j]=inf;
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(G[a][b]>c)
G[a][b]=G[b][a]=c;
}
int ans=dij(1,n);
int cnt=inf;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(G[i][j]<inf&&vis[i][j]==0)
{
cnt=min(cnt,ans+G[i][j]-maxd[i][j]);
}
}
}
if(ans==cnt)
printf("Not Unique!\n");
else
printf("%d\n",ans);
}
return 0;
}

次小生成树(poj1679)的更多相关文章

  1. 次小生成树(POJ1679/CDOJ1959)

    POJ1679 首先求出最小生成树,记录权值之和为MinST.然后枚举添加边(u,v),加上后必形成一个环,找到环上非(u,v)边的权值最大的边,把它删除,计算当前生成树的权值之和,取所有枚举加边后生 ...

  2. POJ1679 The Unique MST[次小生成树]

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28673   Accepted: 10239 ...

  3. POJ1679(次小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36692   Accepted: 13368 ...

  4. POJ1679(次小生成树)

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

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

    题意: 判断最小生成树是否唯一. 思路: 首先求出最小生成树,记录现在这个最小生成树上所有的边,然后通过取消其中一条边,找到这两点上其他的边形成一棵新的生成树,求其权值,通过枚举所有可能,通过这些权值 ...

  6. POJ1679 The Unique MST 【次小生成树】

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

  7. poj1679次小生成树入门题

    次小生成树求法:例如求最小生成树用到了 1.2.4这三条边,总共5条边,那循环3次的时候,每次分别不用1.2.4求得最小生成树的MST,最小的MST即为次小生成树 如下代码maxx即求最小生成树时求得 ...

  8. POJ1679 The Unique MST —— 次小生成树

    题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  9. POJ-1679 The Unique MST,次小生成树模板题

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K       Description Given a connected undirec ...

随机推荐

  1. 21个最佳jQuery插件推荐

    在Javascript应用领域上,使用jQuery可以制作出非常优秀的动画效果,滑块.滑球,以及各种不同的应用.精选出21个最佳的精典案例,如果你是一个前端设计师,一定不要错过. Supersized ...

  2. Python 变量类型和运算符

    -*- coding:utf-8 -*- ''' if语法 if conditon: [tab键] command [tab键] command ... else: [tab键] command [t ...

  3. 【转】MFC 无边框窗口的拖动

    MFC中无边框窗口的拖动 void CXXXXDialog::OnLButtonDown(UINT nFlags, CPoint point) { PostMessage(WM_NCLBUTTONDO ...

  4. circRNA 序列提取中的难点

    在预测circRNA时,都是检测breakpoint 处的reads 数,最后给出的环状RNA的ID 都是诸如 chr14:106994222-107183708 这样的形式,给出了起始和终止位置: ...

  5. c++ 静态成员变量

    在C++中,静态成员是属于整个类的而不是某个对象,静态成员变量只存储一份供所有对象共用.所以在所有对象中都可以共享它.使用静态成员变量实现多个对象之间的数据共享不会破坏隐藏的原则,保证了安全性还可以节 ...

  6. shiro缓存

    shiro的可以权限控制内容包括:URL地址.Web页面的元素.以及方法,即shiro对用户权限的控制是细粒度的.从用户的一次访问来说,他可能需要最多经过三种.多次的验证.这里的多次怎么说呢?如果说W ...

  7. C++类中静态成员函数

    引述自<深入探索C++对象模型>2001:5:1版次,p-150 static member functions的主要特性就是它没有this指针,所以: 1.它不能直接存取其所在class ...

  8. ThinkPHP Mongo驱动update方法支持upsert参数

    Mongo数据库update操作有一个相对于Mysql的关键特性,它可以使用upsert模式,当更新的数据不存在时,直接插入,但是ThinkPHP的Mongo驱动居然不支持这一特性,没办法,自力更生了 ...

  9. ftp命令行工具如何 连接 非标准21端口(其他端口)的ftp服务器

    windows: step1:ftp命令进入ftp交互环境 step2:ftp>open ip空格port 然后...

  10. V4L2规范编程--21

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 资料链接:http://www.cnblogs.com/emouse/archive/2013/ ...