zoukankan      html  css  js  c++  java
  • POJ 1679 The Unique MST (最小生成树 Kruskal )

    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!
    题意
      判断一棵树是否有次小生成树(权值和最小生成树相同),存在就输出No Unique!,不存在就输出最小生成树权值。
    题解:
     
      kruskal算法,首先生成一棵最小生成树,然后枚举加入到树里的边删除,再求MST,如果得到的值相同就证明存在次小生成树。
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    using namespace std;
    const int maxn=105;
    int par[maxn];
    struct edge
    {
        int u,v,cost;
        bool equ,used,del;
    }es[maxn*maxn];
    int n,m;
    bool cmp(edge a,edge b)
    {
        return a.cost<b.cost;
    }
    void init()
    {
        for(int i=1;i<=n;i++)
            par[i]=i;
    }
    int find(int x)
    {
        return x==par[x]?x:par[x]=find(par[x]);
    }
    bool same(int x,int y)
    {
        return find(x)==find(y);
    }
    void unite(int x,int y)
    {
        x=find(x);
        y=find(y);
        if(x!=y)
            par[x]=y;
    }
    bool fi;
    int kruskal()
    {
        init();
        int res=0,cnt=0;
        for(int i=0;i<m;i++)
        {
            edge e=es[i];
            if(!e.del&&!same(e.u,e.v))
            {
                cnt++;
                if(fi)
                    es[i].used=true;
                unite(e.u,e.v);
                res+=e.cost;
            }
        }
        fi=false;
        if(cnt==n-1)
            return res;
        return -1;
    }
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            cin>>n>>m;
            for(int i=0;i<m;i++)
            {
                scanf("%d%d%d",&es[i].u,&es[i].v,&es[i].cost);
                es[i].equ=es[i].used=es[i].del=false;
            }
            for(int i=0;i<m;i++)
                for(int j=0;j<m;j++)
                    if(i!=j&&es[i].cost==es[j].cost)
                        es[i].equ=true;
            sort(es,es+m,cmp);
            fi=true;//只在第一次求MST时标记边是否使用过
            int ans=kruskal();
            bool flag=false;
            for(int i=0;i<m;i++)
            {
                if(es[i].used&&es[i].equ)
                {
                    es[i].del=true;
                    if(ans==kruskal())
                    {
                        cout<<"Not Unique!"<<endl;
                        flag=true;
                        break;
                    }
                    es[i].del=false;
                }
            }
            if(!flag)
                cout<<ans<<endl;
        }
        return 0;
    }
    
    
    
     
  • 相关阅读:
    zabbix 组信息
    perl tk说明
    perl tk说明
    haproxy ssl相关配置
    haproxy ssl相关配置
    haproxy 关闭ssl 3.0 加密
    haproxy 关闭ssl 3.0 加密
    【华为敏捷/DevOps实践】4. 如何从Excel做项目管理的方式中走出来
    【华为云实战开发】13.如何在云端快速搭建python网站
    【华为云实战开发】12.如何在云端快速开展Swagger接口测试
  • 原文地址:https://www.cnblogs.com/orion7/p/7382028.html
Copyright © 2011-2022 走看看