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

http://poj.org/problem?id=1679

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17726   Accepted: 6150

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

 
【题解】:
  用kruskal,求出最小生成树,然后枚举删除最小生成树上的一条边,再求最小生成树,比较前后两次求解的大小
  【注意】:如果题目给的数据不能构成一个树需要输出0
 
【code】:
 
  1 /**
  2 Judge Status:Accepted     Memory:748K
  3 Time:32MS      Language:G++
  4 Code Lenght:1814B    Author:cj
  5 */
  6 #include<iostream>
  7 #include<stdio.h>
  8 #include<string.h>
  9 #include<algorithm>
 10 #include<vector>
 11 
 12 #define N 101
 13 #define M 6000
 14 using namespace std;
 15 
 16 struct Nod
 17 {
 18     int x,y,w;
 19 }node[M];
 20 
 21 int parent[N];
 22 int n,m;
 23 vector<int> vct;
 24 
 25 bool cmp(Nod a,Nod b)
 26 {
 27     return a.w<b.w;
 28 }
 29 
 30 int findp(int a)
 31 {
 32     while(a!=parent[a])
 33     {
 34         a=parent[a];
 35     }
 36     return a;
 37 }
 38 
 39 int merge(Nod nd)  //合并
 40 {
 41     int x = findp(nd.x);
 42     int y = findp(nd.y);
 43     if(x>y)
 44     {
 45         parent[y]=x;
 46         return nd.w;
 47     }
 48     else if(x<y)
 49     {
 50         parent[x]=y;
 51         return nd.w;
 52     }
 53     return -1;
 54 }
 55 
 56 int kruskal(int id)
 57 {
 58     int i,sum=0,cnt=0;
 59     for(i=1;i<=n;i++)   parent[i]=i;
 60     for(i=0;i<m;i++)
 61     {
 62         if(i!=id)
 63         {
 64             int temp = merge(node[i]);
 65             if(temp!=-1)
 66             {
 67                 sum+=temp;
 68                 cnt++;  //剪枝
 69                 if(id==-1)  vct.push_back(i);  //保存第一次最小生成树的各个节点
 70             }
 71             if(cnt>=n-1)  //找到n-1条边即可以跳出了
 72                 break;
 73         }
 74     }
 75     cnt = 0;
 76     for(i=1;i<=n;i++)  //判断是不是构成一棵树
 77         if(parent[i]==i)
 78             cnt++;
 79     if(cnt==1)  //
 80         return sum;
 81     if(id==-1)  //
 82         return 0;
 83     return -1;
 84 }
 85 
 86 int main()
 87 {
 88     int t;
 89     scanf("%d",&t);
 90     while(t--)
 91     {
 92         scanf("%d%d",&n,&m);
 93         int i;
 94         for(i=0;i<m;i++)
 95         {
 96             scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].w);
 97         }
 98         vct.clear();
 99         sort(node,node+m,cmp);
100         int mins = kruskal(-1);  //找到第一颗最小生成树
101         int temp=-1;
102         for(i=0;i<vct.size();i++)
103         {
104             temp = kruskal(vct[i]);  //每次去掉一个节点 再判断是否可以组成最小生成树
105             if(mins==temp)
106                 break;
107         }
108         if(temp==mins)  puts("Not Unique!");
109         else printf("%d
",mins);
110     }
111     return 0;
112 }
原文地址:https://www.cnblogs.com/crazyapple/p/3246187.html