POJ-1679 The Unique MST

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!

题目大意:

给你n个点和m条边让你判断其最小生成树是否唯一

解题思路:

1.对图中每条边,扫描其它边,如果存在相同权值的边,则对该边做标记。

2.然后用kruskal算法求最小生成树

3.求得最小生成树后,如果该生成树未包含作了标记的边,即可判定最小生成树唯一;若含有标记的边,则对所有含标记的边,依次去掉这些边,每次只删去一条边,再求最小生成树,如果求得的最小生成树权值和原来的最小生成树相同,即可判定这个图的最小生成树不唯一。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef struct node{
    int x, y;
    int w, mark;
    bool operator == (node a){
        if(x == a.x && y == a.y && w == a.w && mark == a.mark) return true;
        else return false;
    }
    bool operator < (node a){
        return w < a.w;
    }
}PATH;

const int maxn = 100 + 5;
const int maxm = 10000 + 5;

int n, m;
PATH r[maxm];
int pre[maxn], inx[maxn];

int findfather(int x){
    return pre[x] = (pre[x] == x ? x : findfather(pre[x]));
}
void kruskal(){
    int k, ans, res = 0, cnt = 0, flag = 1;
    memset(inx, 0, sizeof(inx));
    for(int i = 0; i <= n; ++i) pre[i] = i;
    for(int i = 0; i < m; ++i){
        int fx = findfather(r[i].x);
        int fy = findfather(r[i].y);
        
        if(fx == fy) continue;
        pre[fx] = fy;
        res += r[i].w;
        inx[cnt++] = i;
        
        if(cnt == n - 1) break;
    }
    for(int i = 0; i < cnt && flag; ++i){
        if(r[inx[i]].mark) flag = 0;
    }
    
    if(flag) printf("%d
", res);
    else{
        for(int i = 0; i < cnt; ++i){
            ans = 0; k = 0;
            for(int j = 0; j <= n; ++j) pre[j] = j;
            for(int j = 0; j < m; ++j){
                int fx = findfather(r[j].x);
                int fy = findfather(r[j].y);
                
                if(fx == fy) continue;
                if(j == inx[i]) continue;
                pre[fx] = fy;
                ans += r[i].w;
                ++k;
                if(k == n - 1) break; 
            }
            if(ans == res) {
                puts("Not Unique!");
                return;
            }
        }
        printf("%d
", res);
    }
}
int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &n, &m);
        for(int i = 0; i < m; ++i){
            scanf("%d%d%d", &r[i].x, &r[i].y, &r[i].w);
            r[i].mark = 0;
        }
        sort(r, r + m);
        for(int i = 1; i < m; ++i){
            if(r[i].w == r[i-1].w){
                r[i].mark = r[i-1].mark = 1;
            }
        }
        kruskal();
    }
    return 0;
}


原文地址:https://www.cnblogs.com/wiklvrain/p/8179438.html