题目连接

http://poj.org/problem?id=1679

The Unique MST

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

1
6 7
1 3 1
1 2 2
2 3 3
3 4 0
4 6 5
4 5 4
5 6 6
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
1
6 7
1 3 1
1 2 2
2 3 3
3 4 0
4 6 5
4 5 4
5 6 6
0

Sample Output

Not Unique!
287
432
406
326
264
220
273

判断最小生成树是否唯一。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using std::map;
using std::min;
using std::sort;
using std::pair;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 110;
const int INF = 0x3f3f3f3f;
int V, E;
struct edge {
int u, v, w;
inline bool operator<(const edge &x) const {
return w < x.w;
}
}G[(N * N) << 1], X[N * N];
struct Kruskal {
int par[N], rank[N];
inline void init() {
rep(i, V + 1) {
par[i] = i;
rank[i] = 0;
}
}
inline int find(int x) {
while(x != par[x]) {
x = par[x] = par[par[x]];
}
return x;
}
inline bool unite(int x, int y) {
x = find(x), y = find(y);
if(x == y) return false;
if(rank[x] < rank[y]) {
par[x] = y;
} else {
par[y] = x;
rank[x] += rank[x] == rank[y];
}
return true;
}
inline void built() {
int u, v, w;
rep(i, E) {
scanf("%d %d %d", &u, &v, &w);
G[i] = (edge){ u, v, w };
}
}
inline int kruskal_1(int &p) {
init();
int ans = 0;
rep(i, E) {
int u = G[i].u, v = G[i].v;
if(unite(u, v)) {
ans += G[i].w;
X[p++] = (edge){ u, v, G[i].w };
}
}
return ans;
}
inline int kruskal_2(int x, int y) {
init();
int ans = 0;
rep(i, E) {
int u = G[i].u, v = G[i].v;
if(u == x && y == v) continue;
if(unite(u, v)) {
ans += G[i].w;
}
}
return ans;
}
inline void solve() {
built();
sort(G, G + E);
int p = 0, ans = kruskal_1(p);
rep(i, p) {
int ret = kruskal_2(X[i].u, X[i].v);
int t = -1;
for(int j = 1; j <= V; j++) {
if(par[j] == j) t++;
}
if(t) continue;
if(ret == ans) { ans = -1; break; }
}
if(-1 == ans) puts("Not Unique!");
else printf("%d\n", ans);
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &V, &E);
go.solve();
}
return 0;
}

poj 1679 The Unique MST的更多相关文章

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

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

  2. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. c++引用小问题!

    两段程序 string version(const string &s1,const string &s2) { string temp; temp =s2+s1+s2; return ...

  2. django中时区设置

    通过django中的models更新数据库的DateTimeField字段,发现有错误,于是更改了: TIME_ZONE = 'Asia/Shanghai' 结果,还是不正确,于是把: USE_TZ ...

  3. 洛谷P1466 集合 Subset Sums

    P1466 集合 Subset Sums 162通过 308提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 对于从1到N (1 ...

  4. 华为OJ平台——求最大连续bit数

    题目描述: 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1 输入: 一个byte型的数字    输出: 对应的二进制数字中1的最大连续数 思路: ...

  5. [转载]word尾注插入参考文献——前人经验+自己总结

    1. 以尾注的方式插入第一个参考文献. 将光标定位于word文档中将要插入参考文献的位置,按“插入/引用/脚注和尾注”.出现一菜单,选择“尾注”,“文档结尾”,编号格式为“1,2,3”.按“插入”按钮 ...

  6. oracle11g空表不能导出记录

    select 'alter table '||table_name||' allocate extent(size 64k);' from tabs t where not exists (selec ...

  7. python发邮件遇到的端口号问题

    在学习使用python发邮件的过程中, 遇到了一个问题:由于测试的时候使用的是QQ邮箱,要求必须使用SSL/TLS加密,所以有了下面的代码, from email.mime.text import M ...

  8. import的用法

    转自python学习笔记--模块和命名空间 模块(module)是Python中非常重要的一个概念,模块其实就一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的 ...

  9. C# 多线程传参

    using System; using System.Threading; //多线程调试: 2013.10.08 namespace ThreadExample { class App { publ ...

  10. dig out deledted chat messages

    One of my friends asked me to do a favor for her. She said her friend deleted some important chat me ...