POJ-1679 The Unique MST
来源:互联网 发布:java 使用ant design 编辑:程序博客网 时间:2024/06/06 17:48
The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
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'.
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
23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2
————————————————————集训10.1的分割线————————————————————
前言:诶。今天被模拟网络赛了,蓝皮书只做了一道题。
得到了几点教训:
1. 如果会输入顶点的编号(一般都是从1开始),需要考虑是不是还从0开始
2. 模板之类的一定不能出错
3. 不要同时有两种思路做题
思路:这道题考验的是编码能力。。。首先跑第一次Kruskal得到一个MST的权值。标记下这棵MST上有哪些边。然后枚举删除这些边当中的一条,跑一遍Kruskal得到的是不是同样权值的MST。(记得判断删边之后造成了不连通的情况)。
PS.因为存在n = 1的情况,所以从1开始计数。
代码如下:
/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <string>#include <iostream>#define INF 2e9using namespace std;/****************************************/const int N = 105, M = N*N;int n, m, fa[N], same[M];bool use[M], del[M];bool fir;struct Node{int u, v, w;}edge[M];bool cmp(Node a, Node b){return a.w < b.w;}int Find(int x){if(x != fa[x]) fa[x] = Find(fa[x]);return fa[x];}int Kruskal(){for(int i = 1; i <= n; i++)fa[i] = i;int sum = 0, cur = 0;for(int i = 1; i <= m; i++) if(!del[i]) {int fx = Find(edge[i].u), fy = Find(edge[i].v);if(fx != fy) {sum += edge[i].w;fa[fy] = fx;cur++;if(fir) use[i] = true;}if(cur == n-1) break;}int Set = 0;for(int i = 1; i <= n; i++) {if(fa[i] == i) {Set++;//存在1个以上的集合,则不连通if(Set > 1) return -1;}}return sum;}int main(){#ifdef J_Surefreopen("111.in", "r", stdin);//freopen(".out", "w", stdout);#endif int T; scanf("%d", &T); while(T--) {memset(del, 0, sizeof(del));memset(use, 0, sizeof(use));scanf("%d%d", &n, &m);for(int i = 1; i <= m; i++) {scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w);}sort(edge+1, edge+m+1, cmp);fir = true;int ans_st = Kruskal();fir = false;bool ok = true;for(int i = 1; i <= m; i++) if(use[i]) {del[i] = true;int sum2 = Kruskal();if(ans_st == sum2) {ok = false;break;}del[i] = false;}if(ok) {printf("%d\n", ans_st);}else {puts("Not Unique!");}} return 0;}
0 0
- POJ 1679 The Unique MST
- poj 1679 The Unique MST
- poj 1679 The Unique MST
- POJ 1679 The Unique MST
- poj 1679 The Unique MST
- poj 1679 The Unique MST
- POJ-1679 The Unique MST
- POJ 1679 The Unique MST
- POJ 1679 The Unique MST
- POJ-1679-The Unique MST
- Poj 1679 The Unique MST
- poj 1679 The Unique MST
- POJ-1679-The Unique MST
- POJ 1679 The Unique MST
- POJ 1679 The Unique MST
- POJ-1679-The Unique MST
- poj 1679 The Unique MST
- poj 1679-The Unique MST
- QT编写最简单的TCP服务器端(1)
- 三层开发
- hdu 1236 排名
- 五个解决方案让MongoDB拥有RDBMS的鲁棒性事务
- 语音编码-初步了解
- POJ-1679 The Unique MST
- linux核内外通信——ioctl
- 读《程序员修炼之道-从小时工到专家》
- Windows下安装并设置Redis
- ##剑指offer 4.4 分解简化问题1-复杂链表的复制
- linux内核驱动:TQ2440的LED驱动
- addSubview和insertSubview 区别
- work_weipa_activity与fragment跳转问题
- SPring AOP