POJ1679-The Unique MST(次小生成树)

3人阅读 评论(0) 收藏 举报
分类:

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!

题意:是否存在唯一的MST,存在输出MST的权值,不存在输“Not Unique!”。

题解:通过Kruskal构建MST,存储权值t,并对组成MST的边进行标记,。枚举标记过的边将其删除,重新构建MST,存储新权值ans,如果新权值ans等于之前权值t,说明MST不唯一。

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 110;
int n,m;
bool flag;
struct Edge{
 int u,v,w;
 bool del,use;
}edge[maxn*maxn];
int f[maxn];
void init()
{
        for(int i=0;i<n;i++)
                f[i]=i;
}
int Find(int x)
{
        if(x==f[x]) return x;
        return f[x]=Find(f[x]);
}
bool cmp(Edge p,Edge q) {return p.w<q.w;}
int Krusal()
{
        int cnt=0,sum=0;
        init();
        for(int i=0;i<m;i++)
        {
                int xroot=Find(edge[i].u),yroot=Find(edge[i].v);
                if(xroot!=yroot&&edge[i].del==false)
                {
                        if(!flag) edge[i].use=true;
                        f[xroot]=yroot;
                        sum+=edge[i].w;
                        cnt++;
                        if(cnt==n-1)
                        {
                               return sum;
                        }
                }
        }
       return -1;

}
int main()
{
        int t;
        cin>>t;
        while(t--)
        {
          scanf("%d%d",&n,&m);
          if(m==0){ printf("0\n");continue;}
          for (int i=0;i < m;i++)
          {
                  scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
                  edge[i].del = false;
                  edge[i].use = false;
          }
          sort(edge,edge+m,cmp);
          flag=false;
          int t=Krusal();
          flag=true;
          for(int i=0;i<m;i++)
          {
                  if(edge[i].use==true)
                  {
                          edge[i].del=true;
                          int ans=Krusal();
                          if(t==ans)
                          {
                                  t=-1;
                                  break;
                          }
                          edge[i].del=false;

                  }


          }
          if(t==-1)
          {
                  printf("Not Unique!\n");
          }
          else{
                printf("%d\n",t);
          }

        }
}


查看评论

POJ1679-The Unique MST

POJ1679-The Unique MST大致说下题意:有多个case 给出n m , n代表点数,m代表边数,之后给出m条边,该图的最小生成树是不是唯一的。主要做法就是先用prim求出最小生成树...
  • Tylor_ljh
  • Tylor_ljh
  • 2015-11-04 19:20:00
  • 138

POJ1679-The Unique MST(最小生成树)

Description Given a connected undirected graph, tell if its minimum spanning tree is unique. ...
  • KJBU2
  • KJBU2
  • 2014-08-22 15:06:35
  • 361

POJ 1679-The Unique MST( 判断最小生成树的唯一性)

B - The Unique MST Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Sub...
  • zsc2014030403015
  • zsc2014030403015
  • 2015-11-13 13:43:36
  • 695

次小生成树模版(MST)

#include #include #include #include using namespace std; const int MAXNODE = 1010; const int MAX...
  • L123012013048
  • L123012013048
  • 2015-11-04 21:57:33
  • 254

POJ1679 The Unique MST【Kruskal】【次小生成树】

题目大意:给你N个点M条边的图,问:图的最小生成树是否唯一。 思路:参考算法书,在kruskal算法的基础上进行修改,加入(x,y)两点在最小生成树 上路径最长的边的计算。使用了链式前向星记录每个集合...
  • u011676797
  • u011676797
  • 2014-12-28 00:02:05
  • 2334

poj_1679 The Unique MST

The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1...
  • CSDN515
  • CSDN515
  • 2012-08-03 17:17:53
  • 679

The Unique MST 1679(最小生成树的不唯一性)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24076   Accepted: ...
  • yanghui07216
  • yanghui07216
  • 2015-08-13 15:45:20
  • 265

poj1679 The Unique MST--判断MST是否唯一

原题链接:http://poj.org/problem?id=1679 题意: 给出n个点,m条边,判断其最小生成树是否唯一。 思路就是求出最小生成树(MST),再求次小生成树,如果两值...
  • LaoJiu_
  • LaoJiu_
  • 2016-08-09 10:50:17
  • 525

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

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25387  ...
  • qq_29963431
  • qq_29963431
  • 2016-01-12 15:52:14
  • 217
    个人资料
    持之以恒
    等级:
    访问量: 715
    积分: 522
    排名: 10万+
    文章存档
    最新评论