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!

给你一个图 判断其最小生成树是不是唯一的

记录第一个最小生成树的路径,枚举每一条边删除的情况,如果还能再生成一个长度为n-1并且值相等的最小生成树,那么就不是唯一的了

在POJ discuss 找到的一组好样例

9
1 0
4 5
1 2 1
2 3 1
3 4 1
1 4 2
2 4 1
10 15
2 10 97
2 6 18
7 1 63
5 4 62
7 5 93
1 3 10
6 9 99
3 7 73
2 7 6
5 9 22
5 3 82
4 2 36
8 1 50
10 3 20
7 9 69
10 15
10 5 79
4 2 33
4 8 41
9 3 97
5 2 25
2 6 9
2 10 66
8 3 38
10 8 89
1 10 83
1 7 91
7 3 94
7 10 40
7 2 70
2 3 82
10 15
3 8 84
7 10 34
1 10 14
1 9 60
7 6 49
8 5 39
4 5 96
4 7 78
7 3 33
2 8 56
8 9 71
5 2 83
3 6 61
7 9 63
2 6 43
10 15
1 10 25
1 3 14
10 5 72
8 3 18
2 5 41
4 9 86
6 8 17
6 2 98
5 6 34
1 8 90
7 1 65
7 2 63
8 7 71
4 2 64
9 6 50
10 15
2 7 13
5 10 52
5 2 5
10 6 47
9 4 23
8 10 54
1 10 20
4 10 8
6 1 87
8 2 43
8 1 87
6 3 53
3 1 87
2 3 82
4 6 91
10 15
1 2 14
4 1 89
7 6 8
9 4 81
5 2 81
10 9 6
1 5 44
1 3 33
2 6 25
6 10 10
1 10 65
6 9 74
8 10 41
2 3 89
5 10 2
10 15
9 8 14
2 10 66
10 5 73
2 3 98
1 3 30
6 5 3
2 1 84
2 6 33
10 8 24
5 8 34
7 1 69
3 7 60
7 4 38
4 10 65
3 4 32 答案是
0
Not Unique!
287
432
406
326
264
220
273
#include<iostream>
#include<cstdio> //EOF,NULL
#include<cstring> //memset
#include<cstdlib> //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
#include<cmath> //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm> //fill,reverse,next_permutation,__gcd,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#include<iterator>
#include<iomanip> //setw(set_min_width),setfill(char),setprecision(n),fixed,
#include<functional>
#include<map>
#include<set>
#include<limits.h> //INT_MAX
#include<bitset> // bitset<?> n
using namespace std; #define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fori(x) for(int i=0;i<x;i++)
#define forj(x) for(int j=0;j<x;j++)
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(y))
#define all(x) x.begin(),x.end()
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
typedef pair<int,int> P;
typedef long long LL;
typedef long long ll;
const double eps=1e-;
const double PI = acos(1.0);
const int INF = 0x3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int MAXN = 1e6+;
const int maxm = ;
const int maxn = +;
int T;
int n,m;
int cnt,ans,tot,sum,pos;
int pre[maxn];
int path[maxn];
int flag; struct node{
int st,ed,w;
bool operator < (node b) const{
return w < b.w;
}
}rod[maxn]; int find(int x){ return x == pre[x] ? x : pre[x] = find(pre[x]);}
bool join(int x,int y){
if(find(x)!=find(y)){
pre[find(y)] = find(x);
return true;
}
return false;
}
void kruskal(){
for(int i = ; i <= n; i++){
pre[i] = i;
}
for(int i = ;i < m ; i++){
int mp1 = find(rod[i].st);
int mp2 = find(rod[i].ed);
if(join(mp1,mp2)) {
ans += rod[i].w;
path[tot++] = i;
}
}
for(int k = ; k < m ;k++){
for(int i = ; i <= n; i++){
pre[i] = i;
}
sum = pos = ;
for(int i = ; i < m ;i++){
if(i == path[k]) continue;
int mp1 = find(rod[i].st);
int mp2 = find(rod[i].ed);
if(join(mp1,mp2)) {
sum += rod[i].w;
pos++;
}
}
if(pos == n- && sum == ans){
flag = ; break;
}
}
}
int main(){
read(T);
while(T--){
flag = cnt = ans = tot = ;
read2(n,m);
if(n==) {
printf("0\n");
continue;
}
int a,b,c;
for(int i = ; i < m ;i++){
read3(a,b,c);
rod[i].st = a;
rod[i].ed = b;
rod[i].w = c;
}
sort(rod,rod+m);
kruskal();
if(flag)
printf("Not Unique!\n");
else
print(ans);
}
}

POJ 1679 The Unique MST 【判断最小生成树是否唯一】的更多相关文章

  1. poj 1679 The Unique MST 判断最小生成树是否唯一(图论)

    借用的是Kruskal的并查集,算法中的一点添加和改动. 通过判定其中有多少条可选的边,然后跟最小生成树所需边做比较,可选的边多于所选边,那么肯定方案不唯一. 如果不知道这个最小生成树的算法,还是先去 ...

  2. poj 1679 The Unique MST (判定最小生成树是否唯一)

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

  3. POJ 1679 The Unique MST 推断最小生成树是否唯一

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

  4. 【POJ 1679 The Unique MST】最小生成树

    无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成 ...

  5. POJ 1679 The Unique MST(最小生成树)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

  6. POJ 1679 The Unique MST (最小生成树)

    The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...

  7. POJ 1679 The Unique MST 【最小生成树/次小生成树模板】

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

  8. (poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

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

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

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

    题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...

随机推荐

  1. beego 初体验 - 路由

    beego 的路由系统写的很精简 直接将 url 和要映射的 controller 类关联起来就OK: 然后再放一张 login 的 controller 实现,刚好展示下同一个 url 映射到不同方 ...

  2. 记无法用被动方式登录远程linux主机的原因

    [环境]: linux主机:华为企业云 ftp服务端:vsftpd 客户端:ftp命令行工具,安卓端ES文件浏览器 [现象]: 在ES文件浏览器中,使用被动方式没法连接,使用主动方式可以连接,但是没法 ...

  3. djago 定义后台地址

    在app 中urls.py 可替换原始后台登陆地址  /admin   为自定义地址

  4. Vue系列之 => 列表动画

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. C#基本知识零散总结

    C#基本知识零散总结 类的属性:(property) C#中定义属性使用的语法 public string SomeProperty { get { return "属性的值"; ...

  6. session_start 统计实时访客人数

    void Session_Start(object sender, EventArgs e) { Application.Lock(); Application["OnLine"] ...

  7. MVC中视图界面设置Checkbox

    今天是六一儿童节,来谈谈Checkbox,前面的博客已经提到了关于单选.多选.反选.全选等问题的处理,这里作一下补充说明. 全选/反选 <td width="5%">& ...

  8. 【Hadoop学习之十】MapReduce案例分析二-好友推荐

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 最应该推荐的好友TopN,如何排名 ...

  9. intelj idea安装和配置

    1|0优势 intellij idea 是目前公认的java最好的开发工具之一,商业版的IntelliJ应该包含了对 HTML5.CSS3.SASS.LESS.JavaScript.CoffeeScr ...

  10. linux 禁止22端口号

    重启防火墙命令#systemctl restart iptables.service 查看端口号#iptables -L首先修改配置文件 vi /etc/ssh/sshd_config 增加新端口号P ...