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

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!
题意:判断是否存在唯一的最小生成树。
思路:先求出一个最小生成树。在这颗树上先加入(x,y),加入后一定会成环,如果删除(x,y)以外最大的一条边,会得到加入(x,y)时权值最小的一棵树。如果加入的边和删除的边相等,则最小生成不是唯一的。
收获:了解了次小生成树。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 500
int n, m;
int a[maxn][maxn];
int used[maxn][maxn];
int mmax[maxn][maxn];
int vis[maxn];
int dis[maxn];
int pre[maxn];
int prim()
{
int ans = ;
memset(vis, , sizeof vis);
memset(used, , sizeof used);
memset(mmax, ,sizeof mmax);
for(int i = ; i <= n; i++)
{
dis[i] = a[][i];
pre[i] = ;
}
vis[] = ;
dis[] = ;
for(int i = ; i < n; i++)
{
int temp = INF;
int k = -;
for(int j = ; j <= n; j++)
{
if(!vis[j] && temp > dis[j])
{
temp = dis[j];
k = j;
}
}
if(k == -) return ans;
ans += temp;
vis[k] = ;
used[k][pre[k]] = used[pre[k]][k] = ;
mmax[pre[k]][k] = temp;
for(int j = ; j <= n; j++)
mmax[j][k] = max(mmax[j][pre[k]],mmax[pre[k]][k] );//找最大的边权的边。
for(int j = ; j <= n; j++)
{
if(!vis[j] && dis[j] > a[k][j])
{
dis[j] = a[k][j];
pre[j] = k;
}
}
}
return ans;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
int u, v, w;
memset(a, INF, sizeof a);
for(int i = ; i < m; i++)
{
scanf("%d%d%d", &u, &v, &w);
a[u][v] = a[v][u] = w;
}
int mst = prim();
int flag = ;
for(int i = ; i <= n; i++)
for(int j = i+; j <= n; j++)
{
if(a[i][j] == INF || used[i][j] == )
continue;
if(a[i][j] == mmax[i][j])//判断加入的边和删除的边是否相等。
{
flag = ;
break;
}
}
if(flag)
printf("Not Unique!\n");
else
printf("%d\n",mst);
}
return ;
}

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

  1. poj1679次小生成树入门题

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

  2. poj1679 次小生成树

    prim方法:先求过一遍prim,同时标记使用过得边.然后同时记录任意2点间的最大值. 每次加入一条新的边,会产生环,删去环中的最大值即可. #include<stdio.h> #incl ...

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

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

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

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

  5. POJ1679(次小生成树)

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

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

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

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

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

  8. 次小生成树(poj1679)

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

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

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

随机推荐

  1. hdu3870-Catch the Theves(平面图最小割)

    Problem Description A group of thieves is approaching a museum in the country of zjsxzy,now they are ...

  2. Shell下通过echo+telnet在远端执行命令

    创建脚本cmd.sh,用于输入telnet的用户与密码,以及生成远端需要执行的命令   执行命令 MY_SIGN=/tmp/sign; (sh cmd.sh ) | (telnet localhost ...

  3. 《Java程序员面试笔试宝典》之 什么是AOP

    AOP(Aspect-Oriented Programming,面向切面编程)是对面向对象开发的一种补充,它允许开发人员在不改变原来模型的基础上动态地修改模型从而满足新的需求.例如,在不改变原来业务逻 ...

  4. php创建带logo的二维码

    <?php /** php使用二维码 **/ class MyQrcode{ const SIZE = 150; const LEVEL = "L"; const MARGI ...

  5. cocos2d-x 3.0rc开发指南:Windows下Android环境搭建

    安装工具 1. 配置JDK JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 本人的系统是Win7 ...

  6. js中取session的值

    在js中貌似不能取session的值,我在后台设置的session一直拿不到,于是用间接的方式拿到session的值. 首先在jsp中嵌入java代码,用java设置一个变量来取session值,再在 ...

  7. 谈谈UIView的几个layout方法-layoutSubviews、layoutIfNeeded、setNeedsLayout...

    最近在学习swift做动画,用到constraint的动画,用到layoutIfNeeded就去研究了下UIView的这几个布局的方法. 下面是做得一个动画,下载地址:https://github.c ...

  8. hdu 5071 Chat(模拟|Splay)

    Chat Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Sub ...

  9. SQLLoader6(一个或多个数据文件按条件导入不同的表)

    测试一1.创建表: SQL), col2 )); 表已创建. SQL), col2 )); 表已创建. SQL> COMMIT; 提交完成. 2.数据文件:test.txt A A A B B ...

  10. [跟我学spring][Bean的作用域]

    Bean的作用域 什么是作用域呢?即“scope”,在面向对象程序设计中一般指对象或变量之间的可见范围.而在Spring容器中是指其创建的Bean对象相对于其他Bean对象的请求可见范围. Sprin ...