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'.
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!
题意:
首先求出图G的MST,并且看是否唯一。必须注意的一点是图G可能不连通即不存在MST。
思路:
1:先求出图的MST。
2:枚举删除MST中的每条边,重新生成一个树,并记录其总权值,最后取其中最小的,便是次小生成树。
3:如果次小生成树的权值和MST相等且不为0,则输出"Not Unique!",否则输出MST。
AC代码如下:
1 #include<cstdio>
2 #include<iostream>
3 #include<algorithm>
4 using namespace std;
5 #define Max_V 110
6 #define Max_E 5005
7 struct EDGE
8 {
9 int u,v,w;
10 int choose;
11 }edge[Max_E];
12 int path[Max_V],V,E,bj[Max_V+2];
13 bool cmp(const EDGE&a,const EDGE&b)
14 {
15 return a.w<b.w;
16 }
17 void Set()
18 {
19 for(int i=1;i<=V;i++)
20 path[i]=i;
21 return;
22 };
23 int search_path(int x)
24 {
25 if(x!=path[x])
26 path[x]=search_path(path[x]);
27 return path[x];
28 }
29 int Kruskal(int start)
30 {
31 int count=0,sum=0,x,y;
32 Set();
33 for(int i=start;i<E &&count<V-1;i++)
34 {
35 x=search_path(edge[i].u);
36 y=search_path(edge[i].v);
37 if(x==y) continue;
38 bj[count]=i;
39 count++;
40 path[x]=path[y];
41 sum+=edge[i].w;
42 }
43 return count<V-1?0:sum;
44 }
45
46 int Seckruskal()
47 {
48 int count=0,sum=0,x,y;
49 Set();
50 for(int i=0;i<E &&count<V-1;i++)
51 {
52 x=search_path(edge[i].u);
53 y=search_path(edge[i].v);
54 if(x==y||edge[i].choose==1) continue;
55 count++;
56 path[x]=path[y];
57 sum+=edge[i].w;
58 }
59 return count<V-1?0:sum;
60 }
61 int main()
62 {
63 int T,i,flag,temp,MST;
64 scanf("%d",&T);
65 while(T--)
66 {
67 int temp=0;flag=0;
68 memset(bj,0,sizeof(bj));
69 memset(path,0,sizeof(path));
70 memset(edge,0,sizeof(edge));
71 scanf("%d %d",&V,&E);
72 for(i=0;i<E;i++)
73 scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].w);
74 sort(edge,edge+E,cmp);
75 MST=Kruskal(0);
76 for(i=0;i<V-1;i++)
77 {
78 edge[bj[i]].choose=1;
79 temp=Seckruskal();
80 edge[bj[i]].choose=0;
81 if(temp==MST&&temp!=0)
82 {
83 flag=1;
84 break;
85 }
86 }
87 if(flag==0)
88 printf("%d\n",MST);
89 else
90 printf("Not Unique!\n");
91 }
92 return 0;
93 }