poj——The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 30282 | Accepted: 10842 |
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'.
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算法,我们可以知道会出现不同的最小生成树的原因,就在于对于权值相同的边的处理。
代码:
#include<stdio.h> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define N 1000 #define maxn 9999999 using namespace std; int fa[N],ans,tot,answer,n,m,t,x,y,z,p[N]; struct Edge { int x,y,z; }edge[N*N]; int read() { int x=0,f=1; char ch=getchar(); while(ch>'9'||ch<'0') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } int found(int x) { return fa[x]==x?x:fa[x]=found(fa[x]); } int cmp(Edge a,Edge b) { return a.z<b.z; } void begin(int n) { for(int i=1;i<=n;i++) fa[i]=i; } int main() { t=read(); while(t--) { tot=0,ans=0; answer=maxn; memset(fa,0,sizeof(fa)); memset(p,0,sizeof(p)); n=read(),m=read(); for(int i=1;i<=m;i++) { x=read(),y=read(),z=read(); edge[i].x=x; edge[i].y=y; edge[i].z=z; } begin(n); sort(edge+1,edge+1+m,cmp); for(int i=1;i<=m;i++) { x=edge[i].x,y=edge[i].y; int fx=found(x),fy=found(y); if(fx!=fy) { tot++; ans+=edge[i].z; p[tot]=i; fa[fy]=fx; } if(tot==n-1) break; } for(int i=1;i<=tot;i++)//枚举删边 { int k=0,num=0; begin(n); sort(edge+1,edge+1+m,cmp); for(int j=1;j<=m;j++) { if(j==p[i]) continue; x=edge[j].x,y=edge[j].y; int fx=found(x),fy=found(y); if(fx!=fy) { num++; k+=edge[j].z; fa[fy]=fx; } if(num==n-1) break; } if(num==n-1) answer=min(k,answer); } if(answer==ans) cout<<"Not Unique!"<<endl; else cout<<ans<<endl; } }