The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 27141   Accepted: 9712

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!
【分析】最小生成树的唯一性,思路是先判断每条边是否有重边,有的话eq=1,否则0.然后第一次求出最小生成树,将结果记录下来,
然后依次去掉第一次使用过的且含有重边的边,再求一次最小生成树,若结果与第一次结果一样,则不唯一。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
const int M=;
int n,m,cnt;
int parent[N];
bool flag;
struct man {
int u,v,w;
int eq,used,del;
} edg[N];
bool cmp(man g,man h) {
return g.w<h.w;
}
void init() {
for(int i=; i<=; i++) {
parent[i]=i;
}
}
int Find(int x) {
if(parent[x] != x) parent[x] = Find(parent[x]);
return parent[x];
}//查找并返回节点x所属集合的根节点
void Union(int x,int y) {
x = Find(x);
y = Find(y);
if(x == y) return;
parent[y] = x;
}//将两个不同集合的元素进行合并
int Kruskal() {
init();
int sum=;
int num=;
for(int i=;i<m;i++){
if(edg[i].del==)continue;
int u=edg[i].u;int v=edg[i].v;int w=edg[i].w; if(Find(u)!=Find(v)){
sum+=w;
if(!flag)edg[i].used=;
num++;
Union(u,v);
}
if(num>=n-)break;
}
return sum;
}
int main() {
int t,d;
cin>>t;
while(t--) {
cnt=;
cin>>n>>m;
for(int i=; i<m; i++) {
cin>>edg[i].u>>edg[i].v>>edg[i].w;
edg[i].del=;
edg[i].used=;
edg[i].eq=;//一开始这个地方eq没有初始化,WA了好几发,操
}
for(int i=;i<m;i++){
for(int j=;j<m;j++){
if(i==j)continue;
if(edg[i].w==edg[j].w)edg[i].eq=;
}
}
sort(edg,edg+m,cmp);
flag=false;
cnt=Kruskal();
flag=true;
bool gg=false;
for(int i=;i<m;i++){
if(edg[i].used==&&edg[i].eq==){
edg[i].del=;
int s=Kruskal();//printf("%d %d\n",i,s);
if(s==cnt){
gg=true;
printf("Not Unique!\n");
break;
}
edg[i].del=;
}
}
if(!gg)cout<<cnt<<endl;
}
return ;
}

POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)的更多相关文章

  1. POJ-1679 The Unique MST (判断最小生成树的唯一性)

    <题目链接> 题目大意: 给定一张无向图,判断其最小生成树是否唯一. 解题分析: 对图中每条边,扫描其它边,如果存在相同权值的边,则标记该边:用kruskal求出MST. 如果MST中无标 ...

  2. poj1679 The Unique MST(最小生成树唯一性)

    最小生成树的唯一性,部分参考了oi-wiki 如果一条不在最小生成树边集内的边,它可以替换一条在最小生成树边集内,且权值相等的边,那么最小生成树不是唯一的 同过kruskal来判断 考虑权值相等的边, ...

  3. POJ1679:The Unique MST(最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38430   Accepted: 14045 ...

  4. [poj1679]The Unique MST(最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28207   Accepted: 10073 ...

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

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

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

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

  7. POJ1679 The Unique MST 2017-04-15 23:34 29人阅读 评论(0) 收藏

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29902   Accepted: 10697 ...

  8. poj 1679 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. linq.js的用法

    linq.js 详细介绍 linq.js 是一个 JavaScript 实现的 LINQ. 主要特性: 实现所有 .NET 4.0 的方法 complete lazy evaluation full ...

  2. [codeforces] 633C Spy Syndrome 2

    原题 Trie树+dp 首先,我们可以简单的想到一种dp方式,就是如果这一段可以匹配并且可以与前一段接上,那么更新dp[i]为当前字符串的编号,然后倒推就可以得到答案. 但是,显然我们不能O(m)比较 ...

  3. BZOJ 1101 [POI2007]Zap | 第一道莫比乌斯反(繁)演(衍)

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=1101 题解: http://www.cnblogs.com/mrha/p/8203612.h ...

  4. 12.25模拟赛T3

    可以发现,答案O(根号)(因为链上答案最大,n/2,n/3...根号种) 每次求答案要二分 优秀的做法是: 对于小于根号n的暴力nlogn找,可能二分到同一个mid,记忆化一下最小的tot值 对于大于 ...

  5. Different Integers 牛客多校第一场只会签到题

    Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, r ...

  6. 线段树模板 CDOJ1057

    UESTCOJ不知道为什么进不去了哇 跟着叉姐的算法讲堂写的板子 叉姐的思路真的好清晰啊,一定是练习的多并且理解的够深了 希望自己也可以每天进步一点点吧 代码: #include <map> ...

  7. bzoj Gty的超级妹子树 块状树

    Gty的超级妹子树 Time Limit: 7 Sec  Memory Limit: 32 MBSubmit: 500  Solved: 122[Submit][Status][Discuss] De ...

  8. es6+最佳入门实践(8)

    8.Promise 8.1.什么是异步? 要理解异步,首先,从同步代码开始说 alert(1) alert(2) 像上面的代码,执行顺序是从上到下,先后弹出1和2,这种代码叫做同步代码 alert(0 ...

  9. 将数据导入hive,再将hive表导入hbase

    将数据到入hive的无分区表,再将无分区表导入hive的有分区表: --备份 create table tds_package_secinfobk as select * from tds_packa ...

  10. 【poj3734】矩阵乘法

    题解: 若当前有i个格子.2个是偶数的方案数为a[i]1个是偶数的方案数为b[i]0个是偶数的方案数为c[i] a[i+1]=2*a[i](i+1染成黄或蓝)+b[i](把奇数变为偶数)b[i+1]= ...