Time Limit: 1000MS   Memory Limit: 10000K
     

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!

Source

接触的第一道次小生成树的题,还是有很多不足。

题意:t组测试数据,n个点,m条边,求最小生成树是否唯一。

思路:我们知道最小生成树再加入任何一条边都会成环,而且新加的这条边绝对不小于环中的任何一条边。次小生成树就是第二小生成树,与最小生成树差值最小。也就是新加入的边与环中最大边差距尽量小,我们在求最小生成树的时候可以将任意两点间的最大边用邻接矩阵存起来,然后依次遍历所有不在MST中的边替换生成树中环中最大的边。然后得出的最小值便是次小生成树。这里只需判断求出的次小生成树是否等于最小生成树。是则Not
Unique!

const int INF=0x3f3f3f3f;
const int N=200+10;
int n,m,d[N],w[N][N],ma[N][N],vis[N],pre[N],used[N][N];
int prim()
{
memset(vis,0,sizeof(vis));
memset(used,0,sizeof(used));
memset(ma,0,sizeof(ma));
for(int i=1;i<=n;i++) d[i]=w[1][i],pre[i]=1;
vis[1]=1;
pre[1]=-1;
int ans=0;
for(int i=2;i<=n;i++)
{
int x,m=INF;
for(int j=1;j<=n;j++)
if(!vis[j]&&m>d[j])
m=d[x=j];
vis[x]=1;
ans+=m;
used[pre[x]][x]=used[x][pre[x]]=1;
for(int j=1;j<=n;j++)
{
if(vis[j]) ma[j][x]=ma[x][j]=max(ma[pre[x]][j],d[x]);
if(!vis[j]&&d[j]>w[x][j])
d[j]=w[x][j],pre[j]=x;
}
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<N;i++)
for(int j=1;j<N;j++)
w[i][j]=i==j?0:INF;
int u,v,c;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&c);
w[u][v]=w[v][u]=c;
}
int ans=prim();
int res=INF;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i!=j&&!used[i][j]&&w[i][j]!=INF)
res=min(res,ans+w[i][j]-ma[i][j]);//用不在MST中的边替换环中最大的边。
if(res==ans) printf("Not Unique!\n");
else printf("%d\n",ans);
}
return 0;
}

POJ-1679 The Unique MST,次小生成树模板题的更多相关文章

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

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

  2. POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)

    题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...

  3. POJ 1679 The Unique MST (次小生成树kruskal算法)

    The Unique MST 时间限制: 10 Sec  内存限制: 128 MB提交: 25  解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...

  4. poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35999   Accepted: 13145 ...

  5. poj1679The Unique MST(次小生成树模板)

    次小生成树模板,别忘了判定不存在最小生成树的情况 #include <iostream> #include <cstdio> #include <cstring> ...

  6. POJ_1679_The Unique MST(次小生成树模板)

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

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

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

  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 Total Submissions: 22668   Accepted: 8038 D ...

随机推荐

  1. _bzoj1014 [JSOI2008]火星人prefix【Splay】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1014 天,写kth()时,把判断条件k <= siz[ch[x][0]]错写成了k & ...

  2. python_基础部分(1)

    第1章 Python介绍 1.1 基础介绍 l  代码:代码的出现是为了解决生活中的问题 l  编译解释器:目的是让解释器将代码翻译成计算机可识别的语言 l  编程语言:按照一定规则写出来的语言, C ...

  3. Webform 三级联动例子

    首先分别做三个下拉列表 <body> <form id="form1" runat="server"> <asp:DropDown ...

  4. gulp构建工具学习汇总

    前端脚手架____gulp配置文件------- https://pan.baidu.com/s/1eSs7COy 1:有了package.json 直接 npm install自动下载相应的npm包 ...

  5. 行内元素对齐各种问题--从line-height和vertical-align的角度分析

    最近研究行内元素的对齐问题,发现img不管怎么设置,下边都有一块留白,强迫症无法忍受未知,于是开始了查阅探索之旅. 辗转来到张鑫旭的博客,他对行内盒子模型做了详细的介绍,包括“幽灵节点”,“line- ...

  6. ES之各种运算符,for、while、do while 、switch case循环

    运算符优先级: 在所有的运算符中,括号的优先级最高,赋值符号的优先级最低. 小括号 > 计算运算符 > 比较运算符 > 逻辑运算符 > 赋值符号———————————————— ...

  7. 掌握Spark机器学习库-07-随机梯度下降

    1)何为随机梯度下降 优化方法 迭代更新,来寻找函数全局最优解的方法 与最小二乘法相比:适用于变量众多,模型更复杂 2)梯度 变化最快,“陡峭” 通过函数表达式来衡量梯度 3)随机梯度下降原理推导过程 ...

  8. sysbench下载安装

    涉及到sysbench源码的配置和编译,首先确认系统安装了gcc gcc-c++编译器:确认安装了autoconf .automake.libtool等:[root@PC download]# rpm ...

  9. numpy add

    在numpy中,'+' 和add 是一样的 np.add(x1, x2) x1+x2 有种特殊情况需要注意,x1和x2的shape不一样的加法: 两个shape不一样的array相加后会变成一个com ...

  10. vc++实现控制USB设备启用与否

    #include <WINDOWS.H>      #include <TCHAR.H>      #include <SETUPAPI.H>      //#in ...