zoukankan      html  css  js  c++  java
  • (补题 POJ 1679 次小生成树)The Unique MST

    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’.
    InputThe 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.OutputFor 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!
    
    解题思路

    本题大意为删除若干边使每个节点连通的同时保证权值之和最小的方案是否唯一,即判断最小生成树是否唯一。通过分别求最小生成树和次小生成树是否相等。

    这里引入次小生成树的概念。次小生成树就是除了最小生成树之外最小的那个生成树(prime和kruscal算法同样适用与求次小生成树,本讲解主讲kruscal算法实现方法)

    我们先求出最小生成树。把所用的以及权值相同的边分别标记一下,在生成次小生成树的时候如果碰到在最小生成树使用的边还有权值与其相同并且没有杯最小生成树使用的边,就跳过当前使用过的条边选择另一角边(见下图

    img

    代码实现
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    const int N=(int)1e7;
    struct node
    {                                       //eq  标记是否value相同
        int u,v,w;                          //used  标记是否被用过  只是在第一次MST的时候有用
        int eq,used,del;                    //del  在判断生成MST是否为使用的标记
    }e[N];
    int n,first,m;
    int pre[N];
    
    bool cmp(node a,node b)                 //排序 value
    {
        return a.w<b.w;
    }
    int find(int x)                         //找父节点并压缩路径
    {
        if(x!=pre[x])
            pre[x]=find(pre[x]);
        return pre[x];
    }
    int kruskal()                           //kruskal算法
    {
        int i,f1,f2,ans,cnt;
        ans=cnt=0;
        for(i=1;i<=n;i++)
            pre[i]=i;
        for(i=0;i<m;i++)
        {
            if(e[i].del)                    //这就是此次查重过程中要跳过的边
                continue;
            f1=find(e[i].u);
            f2=find(e[i].v);
            if(f1!=f2)
            {
                if(first)
                    e[i].used=1;
                pre[f1]=f2;
                ans+=e[i].w;
                cnt++;
                if(cnt>=n-1)
                    break;
            }
        }
        return ans;
    }
    int main()
    {
        int i,j,T;
        cin>>T;
        while(T--)
        {
            cin>>n>>m;
            for(i=0;i<m;i++)
            {
                cin>>e[i].u>>e[i].v>>e[i].w;
                e[i].del=e[i].eq=e[i].used=0;
            }
            sort(e,e+m,cmp);
            for(i=0;i<m;i++)                //来便利每个边的value是否重样的
            {
                for(j=i+1;j<m;j++)          //是从第一个开始遍历的,看是否有与一重复的
                {                           //然后 从第二个开始遍历,看是否有与二重复的
                    if(e[i].w==e[j].w)      //以此类推就行了
                        e[i].eq=e[j].eq=1;
                    else
                        break;
                }
            }
            first=1;
            int ans=kruskal();
            first=0;
            for(i=0;i<m;i++)
            {
                if(e[i].used&&e[i].eq)      //如果在  *第一次*  被用过并且重复
                {                           //就标记成 1 ,那么在MST(最小生成树)生成中,此边就会被跳过
                    e[i].del=1;             //但是只是重复的边而没有用过
                    if(kruskal()==ans)      //则不会被进行MST查唯一过程
                        break;              //这样就确定了每次的MST都不会和前面的MST一样
                    e[i].del=0;             //每次生成完了之后就把这条边恢复
                }                           //以方便下一次MST把这条边合并进去
            }                               //这里再提一下,每次MST跳过的边都是用过的并且重复的
            if(i<m)                         //只用过了或者只标记重复了是不会被跳过的
                cout<<"Not Unique!"<<endl;
            else
                cout<<ans<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    pandas之DataFrame
    python浅拷贝和深拷贝
    Numpy 机器学习三剑客之Numpy
    django--验证码功能实现
    python基础题
    python武器库
    django-rest-framework
    django--admin组件
    【转载】关于DBUtils中QueryRunner的一些解读
    【转载】java中的反射
  • 原文地址:https://www.cnblogs.com/cafu-chino/p/11759455.html
Copyright © 2011-2022 走看看