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

    The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 21706   Accepted: 7676

    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!
    

    直接用krusual来做。。。

    先做一次最小。。然后记录n-1条边。。

    在求n-1次 , 次小生成树。

    求完次小注意一下要判一下是否有 n-1条边

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    using namespace std ;
    typedef long long LL ;
    const int N = 110;
    const int M = 11000;
    struct edge {
        int u , v , w ;
        bool operator < ( const edge &a ) const {
            return w < a.w ;
        }
    }e[M];
    int n , m , fa[N] , cnt ;
    bool vis[M] ;
    int find( int k ) { return k == fa[k] ? k : find(fa[k]); }
    int mst( int ban  ) {
        int ans = 0 ; cnt = 0 ;
        for( int i = 0 ; i <= n ; ++i ) fa[i] = i ;
        for( int i = 0 ; i < m ; ++i ) {
            int fu = find( e[i].u ) , fv = find( e[i].v );
            if( fu == fv || i == ban ) continue ;
            fa[fv] = fu;
            ans += e[i].w ;
            if( ban == -1 ) vis[i] = true ;
            cnt++ ;
        }
        return ans ;
    }
    
    void Work() {
        sort( e , e + m );
        memset( vis , false , sizeof vis );
        int ans = mst( -1 );
        for( int i = 0 ; i < m ; ++i ) if( vis[i] ) {
            if( mst(i) == ans && cnt == n - 1 ) {
                cout << "Not Unique!" << endl ;
                return ;
            }
        }
        cout << ans << endl ;
    }
    
    int main ()  {
    //    freopen("in.txt","r",stdin);
        int _ ; cin >> _ ;
        while( _-- ) {
            cin >> n >> m ;
            for( int i = 0 ; i < m ; ++i ) {
                cin >> e[i].u >> e[i].v >> e[i].w ;
            }
            Work();
        }
    }
    View Code
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    C++ STL 一般总结(转载)
    Python高级语法总结
    Adaboost 算法的原理与推导——转载及修改完善
    简化版SMO算法标注
    【转载】机器学习算法基础概念学习总结
    C++中添加配置文件读写方法
    Python中scatter()函数--转载
    python 之 strip()--(转载)
    zabbix邮件报警脚本(Python)
    Linux常用命令
  • 原文地址:https://www.cnblogs.com/hlmark/p/4291460.html
Copyright © 2011-2022 走看看