A - The Unique MST poj 1679. 次小生成树

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!

这里我用的kruskal 求的

首先求出最小生成树MST,并记录下树的边到一个数组中。假设有MST中有3条边,分别是1,2,3.

那么就循环3次,分别不用1,2,3,求最小生成树。最后求出最小的那个。就是次小生成树。

比较一下,如果次小生成树和最小生成树相等,那么就不唯一。否则唯一。

#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
using namespace std;
const int maxn=5000;
int w[maxn],u[maxn],v[maxn],p[maxn],r[maxn];
int MST_edge[maxn];int cnt1,cnt2;
int cmp(const int i,int j)
{
    return w[i]<w[j];
}

int find(int x)
{
    return p[x]==x ? x:p[x]=find(p[x]);
}

int kruskal(int n)
{
    int ans=0;
    for(int i=0;i<n;i++) p[i]=r[i]=i;
    sort(r,r+n,cmp);
    for(int i=0;i<n;i++)
    {
        int e=r[i];
        int x=find(u[e]);
        int y=find(v[e]);
        if(x!=y)
        {
            MST_edge[e]=w[e];
            ans+=w[e];
            p[x]=y;
        }
    }
    return ans;
}
int kruskal1(int n,int edge)
{
    int ans=0;
    for(int i=0;i<n;i++) p[i]=r[i]=i;
    sort(r,r+n,cmp);
    for(int i=0;i<n;i++)
    {
        int e=r[i];
        int x=find(u[e]);
        int y=find(v[e]);
        if(x!=y&&e!=edge)
        {
            ans+=w[e];
            p[x]=y;
        }
    }
    return ans;
}
int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        int n,m;scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++) scanf("%d%d%d",&u[i],&v[i],&w[i]);
        int ans1=kruskal(m);int ans2=0x3f3f3f3f;int len=cnt1;
        int dis;
        for(int i=0;i<m;i++)
        {
            if(MST_edge[i]!=0)
            {
                int edge=i;
                dis=kruskal1(m,edge);
            }
            ans2=min(ans2,dis);
        }
        if(ans1==ans2&&ans1!=0) cout<<"Not Unique!"<<endl;
        else  cout<<ans1<<endl;
        memset(u,0,sizeof(u));
        memset(v,0,sizeof(v));
        memset(w,0,sizeof(w));
        memset(p,0,sizeof(p));
        memset(r,0,sizeof(r));
        memset(MST_edge,0,sizeof(MST_edge));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wzazzy/article/details/80957447