The Unique MST

本文介绍了一种判断无向图是否具有唯一最小生成树的方法,提供了两种算法实现思路:Prim算法和Kruskal算法,并附带了完整的AC代码。

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

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 26786 Accepted: 9599
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!

Source

POJ Monthly–2004.06.27 srbga@POJ

题意:
给出一个无向图,请问该无向图的最小生成树是否唯一。若果唯一则输出最小生成树权值,否则输出:Not Unique!
分析:
(1)用prim算法判断是否唯一:当每次选取当前可取最小边时,假设最小值为min=dis[u],如果在已经确定了的顶点中还有另一个顶点到u存在一条等于min的边则说明最小生成树不唯一。(遇到了一题看题解是用求次小生成树的方式判断是否唯一,我用kruskal的方法超时了。)

(2)用kruskal算法判断:将权值相等的边做上标记,算出最小生成树后看这些标记的边是否在生成树里面,若果不在则唯一,若果有,则依次删去这些边,看再次的最小生成树的权值是否和之前的一样。一样则不唯一。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>

using namespace std;

const int maxn=110;
const int maxm=10010;

struct Edge
{
    int u,v,w;
    int equal;
    int used;
    int del;
};



Edge E[maxm];
int p[maxn];
int n,m;

int cmp(Edge a,Edge b)
{
    return a.w<b.w;
}

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

int kruskal()
{
    int ans=0;
    for(int i=0;i<n;i++)
        p[i]=i;
    int num=0;
    for(int i=0;i<m;i++)
    {
        if(E[i].del==1)continue;
        int xx = find_(E[i].u);
        int yy = find_(E[i].v);
        if(xx!=yy)
        {
            p[xx]=yy;
            ans+=E[i].w;
            E[i].used=1;
            num++;
        }
        if(num>=n-1)
            break;
    }
    return ans;
}

int main()
{
    int t,u,v,w;
    scanf("%d",&t);
    while(t--)
    {
       scanf("%d%d",&n,&m);
       for(int i=0;i<m;i++)
       {
           scanf("%d%d%d",&u,&v,&w);
           E[i].u=u-1;
           E[i].v=v-1;
           E[i].w=w;
           E[i].equal=0;
           E[i].used=0;
           E[i].del=0;
       }
       for(int i=0;i<m;i++)
       {
           for(int j=0;j<m;j++)
           {
               if(i==j)continue;
               if(E[i].w==E[j].w)
                E[i].equal=1;
           }
       }
       sort(E,E+m,cmp);
       bool  first=true;
       int w1=kruskal(),w2;
       first = false;
       int i;
       for(i=0;i<m;i++)
       {
           if(E[i].used && E[i].equal)
           {
               E[i].del=1;
               w2=kruskal();
               if(w1==w2)
               {
                   printf("Not Unique!\n");
                   break;
               }
               E[i].del=0;
           }
       }
       if(i>=m)
        printf("%d\n",w1);

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值