poj-1679 The Unique MST
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!
这题是求次小生成树的模板题,思路(之一)是先在读数据的时候,把拥有相同权值的边记录一下,
再跑一遍kruskal,把跑出最小生成树的边都记录下来,因为要判断次小生成树的值和最小生成树
的值是不是相等的,而如果相等,则次小生成树中与之前不同的边一定在之前记录的相同权值的边中
(因为其他的边都要比这些边大),所以只需要依次把最小生成树中满足有其他相同权值边的边去掉再
跑一遍kruskal就行啦。
附代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 const int M = 11111; 8 int pre[M],rk[M]; 9 int V,E,first; 10 struct nod{ 11 int u,v,cost; 12 int vis,del,eq; 13 }eg[M]; 14 bool cmp(const nod&a,const nod&b){ 15 return a.cost<b.cost; 16 } 17 void init(int V){ 18 for(int i=0;i<=V;i++){ 19 pre[i]=i; 20 rk[i]=0; 21 } 22 } 23 int find(int x){ 24 if(pre[x]==x){ 25 return x; 26 } else { 27 return pre[x]=find(pre[x]); 28 } 29 } 30 void mix(int x,int y){ 31 x=find(x); 32 y=find(y); 33 if(x==y) return ; 34 if(rk[x]<rk[y]){ 35 pre[x]=y; 36 } else { 37 pre[y]=x; 38 if(rk[x]==rk[y]){ 39 rk[x]++; 40 } 41 } 42 } 43 int kruskal(){ 44 init(V); 45 sort(eg,eg+E,cmp); 46 int res=0; 47 48 for(int i=0;i<E;i++){ 49 nod e=eg[i]; 50 if(e.del) continue; 51 if(find(e.u)!=find(e.v)){ 52 mix(e.u,e.v); 53 if(first) eg[i].vis=1; 54 res+=e.cost; 55 } 56 } 57 return res; 58 } 59 int main(){ 60 int t; 61 scanf("%d",&t); 62 while(t--){ 63 scanf("%d %d",&V,&E); 64 65 for(int i=0;i<E;i++){ 66 scanf("%d %d %d",&eg[i].u,&eg[i].v,&eg[i].cost); 67 eg[i].del=0;eg[i].vis=0;eg[i].eq=0; 68 } 69 for(int i=0;i<E-1;i++){ 70 for(int j=i+1;j<E;j++){ 71 if(eg[i].cost==eg[j].cost) 72 eg[i].eq=eg[j].eq=1; 73 } 74 } 75 first=1; 76 int ans1=kruskal(); 77 first=0; 78 int i; 79 for( i=0;i<E;i++){ 80 if(eg[i].vis&&eg[i].eq){ 81 eg[i].del=1; 82 int ans2=kruskal(); 83 if(ans2==ans1) break; 84 } 85 eg[i].del=0; 86 } 87 88 if(i!=E){ 89 printf("Not Unique!\n"); 90 } 91 else{ 92 printf("%d\n",ans1); 93 } 94 } 95 return 0; 96 }