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;    }}
原创粉丝点击