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 ?
  • 相关阅读:
    SQL索引是什么?索引的作用是什么?索引的优点是什么?索引的缺点是什么?索引的分类?什么情况下该创建索引?
    Eclipse中JSP生成的class文件去了哪里?
    Jboss 遇到的问题 :org.jboss.tools.vpe.xulrunner.XulRunnerBundleNotFoundException: Bundle org.mozilla.xulrunner.win32.win32.x86_64 is not found.
    Java框架之Hibernate实战篇
    《从0开始学架构》学习笔记(一)
    JavaWeb——Servlet(详细笔记)
    用Eclipse+Maven+Jetty构建Java Web开发环境(详细笔记)
    Jetty 安装、启动与项目部署
    快速排序相关(学习笔记)
    Java中对象的比较(学习笔记)
  • 原文地址:https://www.cnblogs.com/hlmark/p/4291460.html
Copyright © 2011-2022 走看看