1. 程式人生 > >POJ——T1679 The Unique MST

POJ——T1679 The Unique MST

win rep nth bool ans ann tput its inpu

http://poj.org/problem?id=1679

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 30120 Accepted: 10778

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!

Source

POJ Monthly--2004.06.27 [email protected] 很坎坷的一道題:在知道要用次小生成樹之後,先去學的次小生成樹,然後就WA了一上午,下午繼續看~繼續WA, 最後在NINE 大佬糾正下,把42行的return -1改成了return ans~就A了,~~QAQ
 1 #include <algorithm>
 2 #include <cstdio>
 3 
 4 using namespace std;
 5 
 6 const int N(10001);
 7 int num,n,m;
 8 int fa[N],cnt;
 9 int Fir,MST;
10 int u,v,w,used[N];
11 struct Edge
12 {
13     int u,v,w;
14 } edge[N<<1];
15 
16 bool cmp(Edge a,Edge b)
17 {
18     return a.w<b.w;
19 }
20 
21 int find(int x)
22 {
23     return fa[x]==x?x:fa[x]=find(fa[x]);
24 }
25 
26 int Kruskal()
27 {
28     int ans=0; cnt=0;
29     sort(edge+1,edge+m+1,cmp);
30     for(int i=1; i<=n; i++) fa[i]=i;
31     for(int i=1; i<=m; i++)
32     {
33         int fx=find(edge[i].u),fy=find(edge[i].v);
34         if(fx!=fy)
35         {
36             fa[fx]=fy;
37             used[++cnt]=i;
38             ans+=edge[i].w;
39         }
40         if(cnt==n-1) return ans;
41     }
42     return ans;
43 }
44 
45 int SecKru(int cant)
46 {
47     int ans=0; cnt=0;
48     for(int i=1;i<=n;i++) fa[i]=i;
49     for(int i=1;i<=m;i++) 
50     {
51         if(cant==i) continue;
52         int fx=find(edge[i].u),fy=find(edge[i].v);
53         if(fx!=fy)
54         {
55             cnt++;
56             fa[fx]=fy;
57             ans+=edge[i].w;
58         }
59         if(cnt==n-1) return ans;
60     }
61     return 0x7fffffff;
62 }
63 
64 int main() 
65 {
66     scanf("%d",&num);
67     for(;num--;)
68     {
69         scanf("%d%d",&n,&m);
70         for(int i=1;i<=m;i++) 
71             scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
72         Fir=Kruskal(); MST=0x7fffffff;
73         for(int i=1;i<n;i++)
74         {
75             MST=min(SecKru(used[i]),MST);
76         }
77         if(Fir==MST)
78               printf("Not Unique!\n");
79         else  printf("%d\n",Fir);
80     }
81     return 0;
82 }

POJ——T1679 The Unique MST