K - The Unique MST (最小生成树的唯一性)

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!
方法1:首先算出最小生成树的权值和ans,然后枚举删除最小生成树中的每一条边,若还可以达到相同的效果,就说明最小生成树不唯一,
因为两个不同的最小生成树至少有一条边不同,所以我们才可以枚举删除每一条边.
方法2:判断最小生成树和次小生成树的权值是否相同.
#include<iostream>
#include<vector>
#include<algorithm>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int maxn=100010;
int f[maxn];
struct node
{
    int u,v,w;
    bool operator < (const node &r)const{
        return w<r.w;
    }
}q[maxn];
int Find(int x)
{
    return f[x]==x?x:f[x]=Find(f[x]);
}
int Merge(int u,int v)
{
    u=Find(u);
    v=Find(v);
    if(u!=v)return f[u]=v,1;
    return 0;
}
vector<int>v;
int main()
{
    int T;
    cin>>T;
    while(T--){
        v.clear();
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=1;i<=m;i++){
            cin>>q[i].u>>q[i].v>>q[i].w;
        }
        sort(q+1,q+1+m);
        int ans=0;
        for(int i=1;i<=m;i++){
            int x=Merge(q[i].u,q[i].v);
            if(x){
                v.push_back(i);
                ans+=q[i].w;
            }
        }
        int flag=1;
        for(int i=0;i<v.size();i++){
            int sum=0,cnt=0;
            for(int j=1;j<=n;j++)f[j]=j;
            for(int j=1;j<=m;j++){
                if(j==v[i])continue;
                int x=Merge(q[j].u,q[j].v);
                if(x){
                    sum+=q[j].w;
                    cnt++;
                }
            }
            if(cnt==n-1&&ans==sum){
                flag=0;
                break;
            }
        }
        if(flag)cout<<ans<<endl;
        else printf("Not Unique!\n"); 

    }
    return 0;
}

 

#include<iostream>
#include<cstring>

using namespace std;
typedef long long ll;
const int maxn=1010;
const int INF=0x3f3f3f3f;
int Maxlen[maxn][maxn];
int dis[maxn],vis[maxn];
int pre[maxn],MAP[maxn][maxn];
int used[maxn][maxn];
int n,m;

int Prim(int x)
{
    memset(Maxlen,0,sizeof(Maxlen));
    memset(dis,INF,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(pre,0,sizeof(pre));
    memset(used,0,sizeof(used));
    for(int i=1;i<=n;i++){
        dis[i]=MAP[x][i];
        pre[i]=x;
    }
    dis[x]=0;
    vis[x]=1;
    pre[x]=0;
    int ans=0;
    for(int i=2;i<=n;i++){
        int u=0,minn=INF;
        for(int j=1;j<=n;j++){
            if(!vis[j]&&dis[j]<minn){
                u=j;
                minn=dis[j];
            }
        }
        vis[u]=1;
        ans+=minn;
        used[u][pre[u]]=used[pre[u]][u]=1;
        for(int v=1;v<=n;v++){
            if(vis[v]){
                Maxlen[u][v]=Maxlen[v][u]=max(Maxlen[v][pre[u]],dis[u]);
            }
            else{
                if(dis[v]>MAP[u][v]){
                    dis[v]=MAP[u][v];
                    pre[v]=u;
                }
            }
        }
    }
    return ans;
}
void sst(int ans)
{
    int sum=INF;
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            if(!used[i][j]&&MAP[i][j]!=INF){
                sum=min(sum,ans+MAP[i][j]-Maxlen[i][j]);
            }
        }
    }
    if(sum==ans)cout<<"Not Unique!"<<endl;
    else cout<<ans<<endl;
}
int main()
{
    int T;
    cin>>T;
    while(T--){
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(i==j)MAP[i][j]=0;
                else MAP[i][j]=INF;
            }
        }
        for(int i=1;i<=m;i++){
            int u,v,w;
            cin>>u>>v>>w;
            MAP[u][v]=MAP[v][u]=min(MAP[u][v],w);
        }
        int ans=Prim(1);
        sst(ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/cherish-lin/p/11332866.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值