POJ - 1679 The Unique MST(判断最小生成树是否唯一,次小生成树)

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!

题意: 有一些边的关系,判断能构成的最小生成树是否唯一。

思路: 可以从点出发,比如有a,b,c三个点,a,b是已经加入到集合中了的,现在要加入c,如果ac和bc的权值一样都是最小,则说明最小生成树不唯一。

还可以用次小生成树来判断,如果求出的次小生成树的价值等于最小生成树的价值的话就说明最小生成树不唯一;
次小生成树的求解:先用Prim求出最小生成树,在求MST的过程中,记录i到j边权值的最大值,并标记已经加入到MST中的边,然后枚举所有没有加入到MST的边,当加入一条边的时候,根据MST的性质必定会形成环,这时候删除环内权值最大的边(当前加入的边不算),用当前加入的这条边替代,此时求出最小的就是次小生成树,最后判断次小生成树与MST的价值是否相同。

用点来判断:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;

#define ll long long
const int inf = 0x3f3f3f3f;
const int maxn = 1050;
const double pi = acos(-1.0);
const int mod = 1e9+7;

int mp[maxn][maxn],vis[maxn],dis[maxn];
int t,n,m,flag,sum;

void Prim()
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
        dis[i]=mp[1][i];
    vis[1]=1;
    int u,minn;
    for(int i=1;i<n;i++)
    {
        minn=inf;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&dis[j]<minn)
            {
                minn=dis[j];
                u=j;
            }
        }
        if(minn==inf) break;
        int cnt=0;
        for(int j=1;j<=n;j++)
            if(vis[j]&&mp[u][j]==minn)   //记录从u到j与最小权值相同的边的数量 
            cnt++;
        if(cnt>1)   //如果大于1条,就说明存在可以代替的 
        {
            flag=0;
            return ;
        }
        vis[u]=1;
        sum+=dis[u];
        for(int v=1;v<=n;v++)
        {
            if(!vis[v]&&dis[v]>mp[u][v])
                dis[v]=mp[u][v];
        }
    }
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        memset(mp,inf,sizeof(mp));
        int x,y,z;
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d %d",&x,&y,&z);
            mp[x][y]=mp[y][x]=z;
        }
        flag=1;
        sum=0;
        Prim();
        if(flag) printf("%d\n",sum);
        else printf("Not Unique!\n");
    }
    return 0;
}

次小生成树判断:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;

#define ll long long
const int inf = 0x3f3f3f3f;
const int maxn = 1050;
const double pi = acos(-1.0);
const int mod = 1e9+7;

int mp[maxn][maxn],vis[maxn],dis[maxn],pre[maxn],max_e[maxn][maxn],book[maxn][maxn];
int t,n,m,flag,sum;

int Prim()
{
    memset(vis,0,sizeof(vis));
    memset(max_e,0,sizeof(max_e));
    memset(book,0,sizeof(book));
    for(int i=1; i<=n; i++)
    {
        dis[i]=mp[1][i];
        pre[i]=1;
    }
    vis[1]=1;
    int u,minn;
    for(int i=1; i<n; i++)
    {
        minn=inf;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j]&&dis[j]<minn)
            {
                minn=dis[j];
                u=j;
            }
        }
        if(minn==inf) return -1;
        vis[u]=1;
        sum+=dis[u];
        book[u][pre[u]]=book[pre[u]][u]=1;  //加入u这个节点时,标记它前面那个节点到u的这条边已经加入MST中 
        for(int v=1; v<=n; v++)
        {
            if(vis[v])
                max_e[v][u]=max_e[u][v]=max(max_e[v][pre[u]],dis[u]);  //在已经加入到集合的点中更新v到u的最大边权值 
            if(!vis[v]&&dis[v]>mp[u][v]) //如果u到v的距离小于之前到v的距离就更新 
            {
                dis[v]=mp[u][v];
                pre[v]=u;  //把v的上一个节点更新为u 
            }
        }
    }
    return sum;
}

int smst(int sum)
{
    int ans=inf;
    for(int i=1; i<=n; i++)
        for(int j=i+1; j<=n; j++)
            if(mp[i][j]!=inf&&!book[i][j])  //如果i,j之间有边,且没有被加入到MST中,就加入 
                ans=min(ans,sum+mp[i][j]-max_e[i][j]); //把加入边的权值加上,减去原来i到j最大权值的边,求出最小值就是次小生成树 
    if(ans==inf) return -1;
    return ans;
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(i==j) mp[i][j]=0;
                else mp[i][j]=mp[j][i]=inf;
        int x,y,z;
        for(int i=1; i<=m; i++)
        {
            scanf("%d %d %d",&x,&y,&z);
            mp[x][y]=mp[y][x]=z;
        }
        sum=0;
        int ans=Prim();
        if(ans==-1)
        {
            printf("Not Unique!\n");
            continue;
        }
        if(ans==smst(ans)) printf("Not Unique!\n");
        else printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值