1016. Uniqueness of MST (35)

这道题做的麻烦了点
并查集,最小生成树,判断生成树是否唯一稍微麻烦了点

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 505
using 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;

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值