POJ 1679 The Unique MST 确定MST是否唯一

                    The Unique MST

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22817   Accepted: 8114

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]

题意:给出一个图,若图中的MST图只有一个,输入最小代价,若不唯一,输入Not Unique!

O(n^3)

先求出一个MST图,最小代价ans,并且记录MST图的边,然后枚举每一条MST图的边

若去掉该条边后,该图的最小代价还是ans,说明MST图不止一个,则break

最后若枚举每一条边后,新的最小代价总是大于ans

说明该图的MST图只有一个。

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4
  5 using namespace std;
  6
  7 const int MAXN=103;
  8 const int INF=0x3f3f3f3f;
  9
 10 int cost[MAXN][MAXN];
 11 int pre[MAXN];
 12 int lowc[MAXN];
 13 bool vis[MAXN];
 14
 15 void init(int N)
 16 {
 17     for(int i=1;i<=N;i++)
 18     {
 19         for(int j=1;j<=N;j++)
 20         {
 21             if(i==j)
 22                 cost[i][j]=0;
 23             else
 24                 cost[i][j]=INF;
 25         }
 26     }
 27 }
 28
 29 int prim(int N,int cnt)
 30 {
 31     memset(vis,false,sizeof(vis));
 32     if(cnt)
 33         memset(pre,-1,sizeof(pre));
 34     vis[1]=true;
 35     int ans=0;
 36     for(int i=1;i<=N;i++)
 37     {
 38         lowc[i]=cost[1][i];
 39         if(cnt)
 40             pre[i]=1;
 41     }
 42
 43     for(int j=1;j<N;j++)
 44     {
 45         int p=-1;
 46         int minc=INF;
 47         for(int i=1;i<=N;i++)
 48         {
 49             if(!vis[i]&&lowc[i]<minc)
 50             {
 51                 minc=lowc[i];
 52                 p=i;
 53             }
 54         }
 55         if(p==-1)
 56             return -1;
 57         ans+=minc;
 58         vis[p]=true;
 59         for(int i=1;i<=N;i++)
 60         {
 61             if(!vis[i]&&cost[p][i]<lowc[i])
 62             {
 63                 lowc[i]=cost[p][i];
 64                 if(cnt)
 65                     pre[i]=p;
 66             }
 67         }
 68     }
 69     return ans;
 70 }
 71
 72 int main()
 73 {
 74     int test;
 75     scanf("%d",&test);
 76     while(test--)
 77     {
 78         int N,M;
 79         scanf("%d%d",&N,&M);
 80         init(N);
 81
 82         for(int i=0;i<M;i++)
 83         {
 84             int u,v,w;
 85             scanf("%d%d%d",&u,&v,&w);
 86             cost[u][v]=cost[v][u]=w;
 87         }
 88
 89         int ans=prim(N,1);
 90
 91         for(int i=2;i<=N;i++)
 92         {
 93             int v=pre[i];
 94             int tmp=cost[i][v];
 95             cost[i][v]=cost[v][i]=INF;
 96             int sum=prim(N,0);
 97             if(ans==sum)
 98             {
 99                 ans=-1;
100                 break;
101             }
102             cost[i][v]=cost[v][i]=tmp;
103         }
104         if(ans==-1)
105             printf("Not Unique!\n");
106         else
107             printf("%d\n",ans);
108     }
109     return 0;
110 }

时间: 2024-10-12 16:32:45

POJ 1679 The Unique MST 确定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 (判定最小生成树是否唯一)

题目链接: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

poj 1679 The Unique MST 【次小生成树+100的小数据量】

题目地址:http://poj.org/problem?id=1679 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! 分析:T组数据,每组n个节点m条边.计算一下,最小生成树是不是独一无二的,如果是就输出最小生成树的权值和,否则输出Not Unique!(不是独一无二的).先计算最小生成树,在计算次小生成树,判断两者的值是否相等!输入数据保证不存在重边. #include <stdi

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

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

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

链接:poj 1679 题意:判断最小生成树是否唯一, 若唯一,输出最小权值和,否则,输出  Not Unique! 判断最小生成树是否唯一的思路: 1.对图中的每一条边,扫描其他边,如果存在相同权值的边,则对该边做标记 2.然后用Kruskal算法或Prim算法求MST 3.求得MST后,如果该MST中未包含做了标记的边,即可判断MST唯一: 如果包含做了标记的边,则依次去掉这些边的一条边,再求MST, 如果求得的MST权值和原来的MST的权值一样,即可判断MST不唯一. 个人思路是先求最小生