poj1679+次小生成树

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj1679+次小生成树相关的知识,希望对你有一定的参考价值。

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!

题目大意就是求给出的树是否存在唯一的最小生成树。我们可以求出它的次小生成树,如果次小生成树的权等于最小生成树的权,则不唯一。否则唯一。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int mmax=1<<30;
 6 int g[110][110],maxval[110][110],intree[110],eintree[110][110];
 7 int prim(int n)
 8 {
 9     intree[1]=1;
10     int sum=0,i,j,num=0;
11     while(num<n)//朴素prim算法
12     {
13         int mine=mmax,v1,v2;
14         for(i=1;i<=n;i++)
15         {
16             if(intree[i]==1)
17             {
18                 for(j=1;j<=n;j++)
19                 {
20                     if(i!=j&&intree[j]==0)
21                     {
22                         if(g[i][j]<mine)
23                         {
24                             mine=g[i][j];
25                             v1=i;
26                             v2=j;
27                         }
28                     }
29                 }
30             }
31         }
32         if(mine==mmax) break;
33         sum+=mine;
34         num++;
35         intree[v2]=1;
36         eintree[v1][v2]=1;
37         //在prim的同时,用一个矩阵max_val[u][v] 记录在最小生成树(T)中连结任意两点u,v的唯一的路中权值最大的那条边的权值。
38         maxval[v1][v2]=maxval[v2][v1]=g[v1][v2];//这是T中与新加入点直接相连的点
39         for(i=1;i<=n;i++)//T中所有与新加入点间接相连的点
40         {
41             if(intree[i]&&i!=v2&&i!=v1)
42                     maxval[i][v2]=maxval[v2][i]=max(maxval[i][v1],maxval[v1][v2]);
43         }
44     }
45     if(num<n-1) return -1;
46     else return sum;
47 }
48 int main()
49 {
50     int cas;
51     scanf("%d",&cas);
52     while(cas--)
53     {
54         int n,m,i,j;
55         scanf("%d%d",&n,&m);
56         for(i=1;i<=n;i++)
57         {
58             for(j=1;j<=n;j++)
59             {
60                 if(i==j) g[i][j]=0;
61                 else g[i][j]=mmax;
62             }
63         }
64         memset(intree,0,sizeof(intree));//标记点是否在树中
65         memset(eintree,0,sizeof(eintree));//标记边是否在树中
66         memset(maxval,0,sizeof(maxval));
67         for(i=0;i<m;i++)
68         {
69             int a,b,c;
70             scanf("%d%d%d",&a,&b,&c);
71             if(c<g[a][b])
72                 g[a][b]=g[b][a]=c;
73         }
74         int ans=prim(n);
75         int ci=mmax;//ci表示次小生成树的值
76        //枚举所有不在T中的边uv,加入边uv则必然替换权为max_val[u][v]的边,枚举一次就得到一棵新的生成树,如果在这些生成树中有权值和与原最小生成树相等的,则最小生成树不唯一
77         for(i=1;i<=n;i++)
78         {
79             for(j=i+1;j<=n;j++)
80             {
81                 if(!eintree[i][j]&&g[i][j]!=mmax)
82                 {
83                     //printf("%d,%d\n",g[i][j],maxval[i][j]);
84                     int aans=ans+g[i][j]-maxval[i][j];
85                     if(aans<ci) ci=aans;
86                 }
87             }
88         }
89         if(ci==ans) printf("Not Unique!\n");
90         else printf("%d\n",ans);
91     }
92     return 0;
93 }

 


以上是关于poj1679+次小生成树的主要内容,如果未能解决你的问题,请参考以下文章

POJ1679(次小生成树)

POJ 1679 次小生成树

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

POJ-1679 The Unique MST(次小生成树)

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

次小生成树(POJ1679/CDOJ1959)