poj 1679 The Unique MST
题目连接
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的更多相关文章
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- poj 1679 The Unique MST(唯一的最小生成树)
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- poj 1679 The Unique MST (判定最小生成树是否唯一)
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...
- poj 1679 The Unique MST【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24034 Accepted: 8535 D ...
- POJ 1679 The Unique MST (次小生成树kruskal算法)
The Unique MST 时间限制: 10 Sec 内存限制: 128 MB提交: 25 解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- POJ 1679 The Unique MST 推断最小生成树是否唯一
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22715 Accepted: 8055 D ...
随机推荐
- JS实现联想输入(一)
这里是我们的项目中的一个使用JS实现联想输入的功能代码,在此做个小的记录并且将它分享给大家希望对园中的朋友有用! 我将分享三段都非常简单的代码,仅仅作为个人的一点小小的积累而已! 1:后台的Actio ...
- DWR基本配置
DWR——Direct Web Remoter Servlet 供给那些想要以一种简单的方式使用Ajax和XMLHttpRequest的开发者.它具有一套JavaScript功能集,它们把从HTML页 ...
- 洛谷P1631 序列合并
P1631 序列合并 236通过 657提交 题目提供者xmyzwls 标签堆 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 为什么不行? 题目描述 有两个长度都是N的序列A和B,在A和B中 ...
- 使用CORS:跨域两三事
本文为译文. 简介 APIS是可以将富网页应用串连在一起的线程.但是这个应用难以转给浏览器,跨域请求技术的选择被限制了,类似JSONP(由于安全考虑,使用会被限制),或者配置代理(设置和维护都比较头痛 ...
- DuiLib通用窗口类WindowImplBase封装
.h头文件 class WindowImplBase : public CWindowWnd, public INotifyUI, public IMessageFilterUI, public ID ...
- mysql时间日期相加相减实现
分享篇mysql中日期的一些操作,就是我们常常会用到的mysql时间日期的相加或者相减的了,这个mysql也自己带了函数,有需要的朋友可以参考一下. 最简单的方法 select TO_DAYS(str ...
- b2c项目基础架构分析(一)b2c 大型站点方案简述 已补充名词解释
我最近一直在找适合将来用于公司大型bs,b2b b2c的基础架构. 实际情况是要建立一个bs架构b2b.b2c的网站,当然还包括wap站点.手机app站点. 一.现有公司技术人员现状: 1.熟悉asp ...
- 【MySQL】MySQL无基础学习和入门之一:数据库基础概述和实验环境搭建
数据库基础概述 大部分互联网公司都选择MySQL作为业务数据存储数据库,除了MySQL目前还有很多公司使用Oracle(甲骨文).SQLserver(微软).MongoDB等. 从使用成本来区分可以 ...
- 使用python脚本实现基于指定字符串的文本排序
朋友用ansible导出了一个文件,文件中包含上千台机器的磁盘信息,他想要知道哪些机器最需要赶紧扩磁盘.思路是,按剩余磁盘空间百分数,从小到大对文本内容重新排序.下面是具体实现. 源文件ip.txt的 ...
- Unity Js与C#脚本通信
将.js文件放到Standard Assets目录下,否则无法编译通过 CS_test.cs : using UnityEngine; using System.Collections; publ ...