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;
    }
    
    
    
     
  • 相关阅读:
    leetcode 463. Island Perimeter
    leetcode 682. Baseball Game
    leetcode 566. Reshape the Matrix
    Python:SMOTE算法——样本不均衡时候生成新样本的算法
    北京Uber优步司机奖励政策(12月13日)
    滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(12月13日)
    北京Uber优步司机奖励政策(12月12日)
    滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(12月12日)
    北京Uber优步司机奖励政策(12月11日)
    滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(12月11日)
  • 原文地址:https://www.cnblogs.com/orion7/p/7382028.html
Copyright © 2011-2022 走看看