码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1679 The Unique MST(次小生成树)

时间:2015-02-14 13:44:24      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

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

 

POJ 1679 The Unique MST(次小生成树)

标签:

原文地址:http://www.cnblogs.com/hlmark/p/4291460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!