POJ——T1679 The Unique MST

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 }
时间: 2024-10-06 23:17:45

POJ——T1679 The Unique MST的相关文章

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 followin

poj 1679 The Unique MST (判断最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20679   Accepted: 7255 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1679 The Unique MST(求最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20430   Accepted: 7186 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

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

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20293   Accepted: 7124 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

poj 1679 The Unique MST 【次小生成树】【模板】

题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后添加到最小生成树上,然后把原树上添加了之后形成环的最长的边删去,知道一个最小的.就是次小生成树. 这些需要的都可以在求解最小生成树的时候处理出来. AC代码: #include <cstdio> #include <cstring> #include <iostream> #i

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

C - The Unique MST Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1679 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree):

POJ 1679 The Unique MST【MST是否唯一,Prime算法,最好的代码】

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27389   Accepted: 9816 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1679 The Unique MST

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29079   Accepted: 10398 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undir

poj 1679 The Unique MST (判定最小生成树是否唯一)

题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29408   Accepted: 10520 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spannin