The Unique MST——最小生成树(判断最小生成树的唯一性)

题目链接

题意:

给一个图,问其最小生成树是否唯一。

题解:

用Kruskal 算出最小生成树的值,并记录每一条边,然后枚举去掉这些边 看其是否也能构成最小生成树且值相同。

注意 在删边后,可能图构不成一棵树,得判断一下。

代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = 10005;
int f[maxn];
int n,cnt,m;
int vis[maxn];
struct node
{
    int u,v;
    int w;
    bool operator < (const node &a)const
    {
        return w<a.w;
    }
} edge[maxn];

int Find(int x)
{
    return x==f[x]?x:f[x]=Find(f[x]);
}
void add(int u,int v,int w)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt++].w=w;
}

void kruskal()
{
    int ans=0;
    int cntt=0;
    int flag=1;
    for(int i=0; i<=n; i++)f[i]=i;
    sort(edge,edge+cnt);
    for(int i=0; i<cnt; i++)
    {
        int x=edge[i].u;
        int y=edge[i].v;
        int fx=Find(x);
        int fy=Find(y);
        if(fx!=fy)
        {
            f[fx]=fy;
            vis[cntt++]=i;
            ans+=edge[i].w;

        }
    }

    for(int i=0;i<cntt;i++)
    {
        for(int k=0; k<=n; k++)f[k]=k;
        int sum=0,res=0;
        for(int j=0;j<cnt;j++)
        {
            if(j==vis[i])continue;
            int x=edge[j].u;
            int y=edge[j].v;
            int fx=Find(x);
            int fy=Find(y);

            if(fx!=fy)
            {
                f[fx]=fy;
                res+=edge[j].w;
                sum++;
            }
        }
        if(ans==res && sum==n-1){flag=0;break;}///判断是否能构成树 且 是否与最小生成树相等
    }
    if(flag)printf("%d\n",ans);
    else printf("Not Unique!\n");

}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        cnt=0;
        for(int i=1; i<=m; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
        }
        kruskal();

    }

    return 0;
}

kruskal

原文地址:https://www.cnblogs.com/j666/p/11617275.html

时间: 2024-10-19 17:41:16

The Unique MST——最小生成树(判断最小生成树的唯一性)的相关文章

poj 1679 The Unique MST (判断最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20679   Accepted: 7255 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

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

题目链接: http://poj.org/problem?id=1679 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

[kuangbin带你飞]专题六 最小生成树 K - The Unique MST (判断最小生成树是否唯一)

K - The Unique MST 题目链接:https://vjudge.net/contest/66965#problem/K 题目: 给定连接的无向图,告诉它的最小生成树是否唯一. 定义1(生成树):考虑连通的无向图G =(V,E). G的生成树是G的子图,比如T =(V',E'),具有以下属性:    1. V'= V.    2.T是连接的和非循环的. 定义2(最小生成树):考虑边加权,连通,无向图G =(V,E). G的最小生成树T =(V,E')是总成本最小的生成树. T的总成本

POJ 1679 The Unique MST(判断最小生成树_Kruskal)

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 followin

POJ 1679 The Unique MST(求最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20430   Accepted: 7186 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27141   Accepted: 9712 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

The Unique MST (判断是否存在多个最小生成树)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24058   Accepted: 8546 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1679 The Unique MST (Kruskal 判最小生成树是否唯一)

The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21646 Accepted: 7661 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undirected

POJ 1679 The Unique MST(推断最小生成树_Kruskal)

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 followin

poj1679The Unique MST判断最小生成树是否唯一以及求次小生成树边权和的讲解

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22346   Accepted: 7924 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undirected graph G =