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

[2016-01-27][POJ][1679][The Unique MST]

时间:2016-01-28 00:27:47      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

[2016-01-27][POJ][1679][The Unique MST]

C - The Unique MST
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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!
  • 时间:2016-01-21  11:06:30  星期四
  • 题目编号:POJ 1679
  • 题目大意:给出一个图,判断它的最小生成树 是否是唯一的.
  • 方法:在依次删除一条最小生成树的一条边,重新跑最小生成树,  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const int maxn = 110;
int n,m;
 
struct Edge{
    int u,v;
    long long c;
    bool operator < (const Edge & a)const
    {
        return c < a.c;
    }
}e[maxn*maxn/2];
 
int fa[maxn];
void ini(){
    for(int i = 0;i < n + 5;i++)
        fa[i] = i;
}
 
int fnd(int x){
    return x == fa[x] ? x : (fa[x] = fnd(fa[x]));
}
vector<Edge> chs;
int kruskal()
{
    sort(e,e + m);
    int ct = 0;
    long long res = 0;
    ini();
    chs.clear();
    for(int i = 0;i < m;i++)
    {
        if(ct == n - 1)
            break;
        int u = e[i].u,v = e[i].v;
        u = fnd(u);
        v = fnd(v);
        if(u != v)
        {
            fa[u] = v;
            res += e[i].c;
            chs.push_back(e[i]);
            ct++;
        }
    }
 
 
    /****************************************/
 
    for(int j = 0;j < chs.size();j++)
    {
        long long rres = 0;
        ini();
        ct = 0;
        for(int i = 0;i < m;i++)
        {
            if(ct == n - 1)
                break;
            if( chs[j].v == e[i].v  && chs[j].u == e[i].u && chs[j].c == e[i].c)
                continue;
 
            int u = e[i].u,v = e[i].v;
            u = fnd(u);
            v = fnd(v);
            if(u != v)
            {
                fa[u] = v;
                rres += e[i].c;
                ct++;
            }
        }
        if (rres == res && ct == n - 1)
        {
            res = -1;
            break;
        }
    }
 
    /*******************************************/
    return res;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        int a,b,c;
        for(int i = 0;i < m ;i++){
            scanf("%d%d%d",&a,&b,&c);
            e[i].u = a;
            e[i].v = b;
            e[i].c = c;
        }
        int res = kruskal();
        if(res != -1)
            printf("%d\n",res);
        else puts("Not Unique!");
    }
    return 0;
}







[2016-01-27][POJ][1679][The Unique MST]

标签:

原文地址:http://www.cnblogs.com/qhy285571052/p/5164843.html

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