poj1679 The Unique MST(判定次小產生樹),poj1679mst
The Unique MST
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 23180 |
|
Accepted: 8235 |
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
23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2
Sample Output
3Not Unique!
Source
POJ Monthly--2004.06.27 srbga@POJ
對於最小產生樹(可以用kruskal和prime演算法求得,在這裡我是用kruskal求得,如果不會請自己百度。),邊的權值的和最小稱為最小產生樹。
而次小產生樹就是除了最小產生樹外的最小產生樹。而且所有的次小產生樹都是通過最小產生樹的換邊得到的。
所以痛點就是如何換邊。
對於如何換邊:
1.先求出最小產生樹,值為x。
2.一一枚舉添加不在產生樹上的邊(這時候一定形成了一個環)
3.尋找環上的(最小產生樹上的邊)權值最大值與你所添加不在產生樹上的邊的權值比較,所得到的差值為min。
由於是一一枚舉添加邊,min有多個,求出最小的哪一個,所以次小產生樹就為x+min。
昨天雖然把這道題A了,可是看到討論區的測試資料發現自己又一個沒有過,然而卻AC了。然後今天起床就來研究研究。。。
發現我的程式是在找最大值,可是如果一個環有分支,它還會去找分支裡面的最大值,於是就又最佳化了一下。
用的優先隊列。
先附上第一次做的代碼:
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;struct node {int a,b,cost;}c[10005];int fa[105],tree[105][105],vis[10005],vis_tree[105];//vis數組是對m對資料的標記vis_tree是對最小產生樹標記int n,m,max1;bool cmp(node x,node y){if(x.cost<y.cost) return true;elsereturn false;}int find(int x)//尋找根{if(fa[x]!=x) fa[x]=find(fa[x]);return fa[x];}void sec_tree(int a,int b)//尋找產生樹某條邊的最大值(我在這裡做的是錯誤的,如果形成的環有分支,也會尋找){vis_tree[a]=1;if(a==b)return ;for(int i=1;i<=n;i++)if(tree[a][i]&&!vis_tree[i]){if(max1<tree[a][i])max1=tree[a][i];sec_tree(i,b);}}int main(){int ncase;scanf("%d",&ncase);while(ncase--){memset(vis,0,sizeof(vis));memset(tree,0,sizeof(tree));memset(&c,0,sizeof(&c));scanf("%d %d",&n,&m);for(int i=1;i<=n;i++)fa[i]=i;for(int i=0;i<m;i++)scanf("%d %d %d",&c[i].a,&c[i].b,&c[i].cost);sort(c,c+m,cmp);int sum=0;for(int i=0;i<m;i++)//kruskal演算法求最小產生樹{int x=find(c[i].a);int y=find(c[i].b);if(x!=y){fa[x]=y,sum+=c[i].cost;tree[x][y]=tree[y][x]=c[i].cost;vis[i]=1;}}int flag=0;for(int i=0;i<m;i++){if(!vis[i])//不在產生樹中的邊和形成的環的最大值比較。如果相等,MST不唯一{max1=-1; memset(vis_tree,0,sizeof(vis_tree));sec_tree(c[i].a,c[i].b);if(max1==c[i].cost){flag=1;break;}}}if(!flag)printf("%d\n",sum);elseprintf("Not Unique!\n");}}
這是最佳化後的代碼,注釋和上面一樣,就一個地方不同:
#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>using namespace std;struct node1{int a,b,cost;friend bool operator<(node1 x,node1 y ){return x.cost<y.cost;}};priority_queue<node1>s;struct node {int a,b,cost;}c[10005];int fa[105],tree[105][105],vis[10005],vis_tree[105];int n,m,max1,flag1;bool cmp1(node x,node y){if(x.cost<y.cost) return true;elsereturn false;}int find(int x){if(fa[x]!=x) fa[x]=find(fa[x]);return fa[x];}void sec_tree(int a,int b){node1 temp;vis_tree[a]=1;if(a==b)//如果找到a=b,標記一下{flag1=1;}for(int i=1;i<=n;i++)if(tree[a][i]&&!vis_tree[i]){temp.a=a,temp.b=i,temp.cost=tree[a][i];s.push(temp);if(!flag1)//就是在這裡和上面不同,如果找不到a=b,那麼就把以前的恢複s.pop(),vis_tree[i]=0,sec_tree(i,b);}}int main(){int ncase;scanf("%d",&ncase);while(ncase--){memset(vis,0,sizeof(vis));memset(tree,0,sizeof(tree));memset(&c,0,sizeof(&c));scanf("%d %d",&n,&m);for(int i=1;i<=n;i++)fa[i]=i;for(int i=0;i<m;i++)scanf("%d %d %d",&c[i].a,&c[i].b,&c[i].cost);sort(c,c+m,cmp1);int sum=0;for(int i=0;i<m;i++){int x=find(c[i].a);int y=find(c[i].b);if(x!=y){fa[x]=y,sum+=c[i].cost;tree[x][y]=tree[y][x]=c[i].cost;vis[i]=1;}}int flag=0;for(int i=0;i<m;i++){if(!vis[i]){int flag1=0;while(!s.empty())s.pop();memset(vis_tree,0,sizeof(vis_tree));sec_tree(c[i].a,c[i].b);node1 temp;temp=s.top();if(temp.cost==c[i].cost){flag=1;break;}}}if(!flag)printf("%d\n",sum);elseprintf("Not Unique!\n");}}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。