poj 1679 The Unique MST 【次小生成树】

本文针对POJ 1679题目,讲解了如何判断给定图的最小生成树是否唯一的方法。通过定义生成树和最小生成树的概念,介绍了解题思路和算法实现,包括Prim算法的使用,并提供了完整的C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://poj.org/problem?id=1679

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 36667 Accepted: 13356

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

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!

题意:让你判断最小生成树是否是唯一的,如果是唯一的,输出最小生成树的权值(将所有点连通需要的最低花费),如果不是的话,输出'Not Unique!'。

思路:最小生成树都知道是啥,那么次小生成树其实就是除了小于最小生成树的那些生成树中最小的生成树。这道题就是问你最小生成树是否唯一,也就是是否存在其他的生成树方式使得其权值之和和最小生成树权值一样,如果一样,输出'Not Unique!',否则输出生成树最小权值之和。

推荐两篇大神博客:帅气的小牛!和 思路来源者,扣波六!

附上一下自己的代码:

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 120;
const int inf = 0x3f3f3f3f;
int map1[maxn][maxn];
int vis[maxn],dis[maxn];
int mst[maxn][maxn]; //mst中i和j的最大边权
int pre[maxn]; //记录前驱
int inmst[maxn][maxn]; // 标记该边是否在mst中
int n,m,u,v,w,ans1;
void solve()
{
    memset(inmst,0,sizeof inmst);
    memset(mst,0,sizeof mst);
    memset(vis,0,sizeof vis);
    for(int i=1;i<=n;i++)
    {
        dis[i] = map1[1][i];
        pre[i] = 1; //不同之处,每个点的前驱结点先设置为1
    }
    vis[1] = 1;
    int ans,next;
    ans1 = 0;
    for(int i=1;i<n;i++)
    {
        ans = inf;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j] && dis[j] < ans)
            {
                ans = dis[j];
                next = j;
            }
        }
        ans1+=ans;
        vis[next] = 1;
        int father = pre[next]; //找到当前节点的前驱结点
        inmst[next][father] = inmst[father][next] = 1; //在mst中
        for(int j=1;j<=n;j++)
        {
            if(!vis[j] && dis[j] > map1[next][j])
            {
                dis[j] = map1[next][j];
                pre[j] = next; //更新前驱结点
            }
            if(vis[j] && j!=next) //mst中的点
                //下面一步的作用是找到j和next这两个点之间最长的路径(不会包括刚添进去的边)
                mst[j][next] = mst[next][j] = max(mst[father][j],dis[next]);
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int t,flag;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i==j) map1[i][j] = map1[j][i] = 0;
                else map1[i][j] = map1[j][i] = inf;
            }
        }
        for(int i=1;i<=m;i++)
        {
            cin>>u>>v>>w;
            map1[u][v] = map1[v][u] = w;
        }
        flag = 0;
        solve();
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<i;j++)
            {
                //下面的一步的作用是判断i和j之间是否有边,和i,j是否在最小生成树中
                if(map1[i][j] != inf && !inmst[i][j])
                {
                    if(map1[i][j] == mst[i][j])
                    {
                        cout<<"Not Unique!"<<endl;
                        flag = 1;
                    }
                }
            }
        }
        if(flag!= 1)
            cout<<ans1<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值