1016. Uniqueness of MST (35)
来源:互联网 发布:学python和java哪个好 编辑:程序博客网 时间:2024/06/05 07:53
这道题做的麻烦了点
并查集,最小生成树,判断生成树是否唯一稍微麻烦了点
1016. Uniqueness of MST (35)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
Given any weighted undirected graph, there exists at least one minimum spanning tree (MST) if the graph is connected. Sometimes the MST may not be unique though. Here you are supposed to calculate the minimum total weight of the MST, and also tell if it is unique or not.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 2 numbers N (<= 500), and M, which are the total number of vertices, and the number of edges, respectively. Then M lines follow, each describes an edge by 3 integers:
V1 V2 Weight
where V1 and V2 are the two ends of the edge (the vertices are numbered from 1 to N), and Weight is the positive weight on that edge. It is guaranteed that the total weight of the graph will not exceed 230.
Output Specification:
For each test case, first print in a line the total weight of the minimum spanning tree if there exists one, or else print “No MST” instead. Then if the MST exists, print in the next line “Yes” if the tree is unique, or “No” otherwise. If there is no MST, print the number of connected components instead.
Sample Input 1:
5 7
1 2 6
5 1 1
2 3 4
3 4 3
4 1 7
2 4 2
4 5 5
Sample Output 1:
11
Yes
Sample Input 2:
4 5
1 2 1
2 3 1
3 4 2
4 1 2
3 1 3
Sample Output 2:
4
No
Sample Input 3:
5 5
1 2 1
2 3 1
3 4 2
4 1 2
3 1 3
Sample Output 3:
No MST
2
#include<iostream>#include<algorithm>#include<vector>#define MA 505using namespace std;int pre[MA];class edge {public: int a, b, l; int f=1; bool operator<(const edge b) const { return l < b.l; }};int findroot(int index){ if (pre[index] != index) pre[index] = findroot(pre[index]); return pre[index];}void A_union(int a, int b) { if (findroot(a) > findroot(b)) pre[pre[a]] = pre[pre[b]]; else pre[pre[b]] = pre[pre[a]];}int main(){ int N,M; cin >> N>>M; vector<edge> all; for (int t = 0; t <= N; ++t) pre[t] = t; while (M--) { edge temp; cin >> temp.a >> temp.b >> temp.l; all.push_back(temp); } int A = -1, flag = 0,sum=0,num=1; sort(all.begin(), all.end()); for (int i=0;i<all.size();++i) { auto x = all[i]; if (findroot(x.a) != findroot(x.b)){ int j = i + 1; A = x.l; while (all[j].l == A) { if (findroot(all[j].a) == findroot(all[j].b)) all[j].f = 0; ++j; } A_union(x.a, x.b); sum += x.l; ++num; } else if(x.f) { if (A == x.l) flag = 1; if (num == N) break; } } if (num == N) { cout << sum << endl; if (flag) cout << "No" << endl; else cout << "Yes" << endl; } else { int AA[MA] = {}; num = 0; for(int t=1;t<=N;++t) if (!AA[findroot(t)]) { AA[findroot(t)] = 1; ++num; } cout << "No MST" << endl << num << endl; }}
- 1016. Uniqueness of MST (35)
- 1016. Uniqueness of MST (35)
- pat-top 1016. Uniqueness of MST (35)
- 1016. Uniqueness of MST (35)解题报告
- PAT (Top Level) Practise 1016 Uniqueness of MST (35)
- 【次小生成树】PAT (Top Level) 1016 Uniqueness of MST (35)
- The Uniqueness of Software Engineering: The Dual Formalizations
- 20060410-Quick tip: Determining uniqueness of local maximum
- array summation and merging based on uniqueness of element at certain position + ruby arrays
- POJ 2395 Out of Hay (MST)
- MST
- MST
- MST......
- mst
- MST
- MST
- MST
- constraint name uniqueness
- A
- 数码大师2010免费版
- SQL中EXISTS的用法
- Tensorflow:深度神经网络DNN预测波士顿房价(boston house price)【二】
- leetcode -- 14. Longest Common Prefix
- 1016. Uniqueness of MST (35)
- JAVA继承与多态概述
- 编译caffe windows vs2013 Nuget 无法下载 依赖包
- 算法之选择排序算法(golang)
- 方法的调用和类和对象基础的小例子
- 二叉搜索树
- ubuntu中让echo命令显示带颜色的字
- 命令窗口的打开
- Nginx系列—重要算法介绍