zoukankan      html  css  js  c++  java
  • POJ1679:The Unique MST(最小生成树)

    The Unique MST

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 38430   Accepted: 14045

    题目链接:http://poj.org/problem?id=1679

    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:

    2
    3 3
    1 2 1
    2 3 2
    3 1 3
    4 4
    1 2 2
    2 3 2
    3 4 2
    4 1 2

    Sample Output:

    3
    Not Unique!

    题意:

    判断最小生成树是否唯一,如果是就输出最小生成树的边权和。

    题解:

    对于权值相同的边,先把不能加入的边去除掉,然后把能加的边都加进图中,如果还剩有边,那么说明最小生成树不是唯一的。

    代码如下:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <queue>
    using namespace std;
    typedef long long ll;
    const int N = 105;
    int t,n,m;
    struct Edge{
        int u,v,w;
        bool operator < (const Edge &A)const{
            return w<A.w;
        }
    }e[N*N];
    int f[N];
    int find(int x){
        return f[x]==x?f[x]:f[x]=find(f[x]);
    }
    int Kruskal(){
        int ans=0,cnt,j;
        for(int i=0;i<=n+1;i++) f[i]=i;
        for(int i=1;i<=m;i++){
            j=i;cnt=0;
            while(e[i].w==e[j].w && j<=m) j++,cnt++;
            for(int k=i;k<j;k++){
                int fx=find(e[k].u),fy=find(e[k].v);
                if(fx==fy) cnt--;
            }
            for(int k=i;k<j;k++){
                int fx=find(e[k].u),fy=find(e[k].v);
                if(fx!=fy){
                    f[fx]=fy;
                    ans+=e[i].w;
                    cnt--;
                }
            }
            if(cnt>0) return -1;
        }
        return ans ;
    }
    int main(){
        cin>>t;
        while(t--){
            scanf("%d%d",&n,&m);
            for(int i=1;i<=m;i++){
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                e[i].u=u;e[i].v=v;e[i].w=w;
            }
            sort(e+1,e+m+1);
            int ans = Kruskal();
            if(ans==-1) puts("Not Unique!");
            else printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    02.两数相加
    1480. 一维数组的动态和
    01. 两数之和
    67.二进制求和
    文献阅读(QoS-Aware Web Service Recommendation by Collaborative Filtering)
    服务计算---课程总结报告
    125. 验证回文串
    Docker 常用命令
    Docker 安装
    RESTful
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/10372197.html
Copyright © 2011-2022 走看看