POJ 1679 The Unique MST 次小生成树模板题

36 篇文章 0 订阅
4 篇文章 0 订阅
The Unique MST
Time Limit: 1000MS      Memory Limit: 10000K
Total Submissions: 28409        Accepted: 10153

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

在图G的最小生成树T 中 如果往T加入一条不属于T 但属于G的边e
就得到一个包含边e的环 在环中删除掉除了e以外的任意一条边 就得到一棵新的生成树 显然 对加入边e的情况 删除环中除了e外 权值的最大的边 得到的生成树最小
枚举每条不属于T的边 并删去环中权值最大的边 就得到图G的次小生成树
不难理解 kruskal算法在加入一条边e={a,b,w}a所在集合必定与b所在集合不联通
且任意一点u属于a所在集合 任意一点v属于b所在集合 在加入该边后 uv之间的路径中 边的权值最大就是w了(e之前加入的边 权值都比e小)
那么在kruskal算法运行时 就可以得到任意2点间权值最大的边

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int N=100+5;

struct edge{
    int a,b,w;
    bool selected;//是否包含在最小生成树中
};

vector<edge>vedge;
vector<int>G[N];//G[i]记录所有与i联通的点 (i所在集合的点)
int par[N];//并查集
int len[N][N];//len[i][j]:i<->j路径中权值最大的边权
int mst;//最小生成树值
int secmst;//次小生成树值

inline int find(int x){
    return x==par[x]?x:par[x]=find(par[x]);
}

inline void merge(int x,int y){
    par[find(y)]=find(x);
}

bool comp(const edge&a,const edge&b){
    if(a.w!=b.w)
        return a.w<b.w;
    if(a.a!=b.a)
        return a.a<b.a;
    return a.b<b.b;
}

void kruskal(int n,int m){
    for(int i=1;i<=n;++i){
        G[i].clear();
        G[i].push_back(i);//i所在的集合 必定包含i
        par[i]=i;//init并查集
    }
    sort(vedge.begin(),vedge.end(),comp);
    int num=mst=0;
    for(int i=0;i<m;++i){
        if(num==n-1)
            break;
        int a=find(vedge[i].a),
                b=find(vedge[i].b),
                w=vedge[i].w;
        if(a!=b){
            ++num;
            mst+=w;
            merge(a,b);
            vedge[i].selected=true;
            for(int i=0;i<G[a].size();++i)//记录2点间最大权值
                for(int j=0;j<G[b].size();++j)
                    len[G[a][i]][G[b][j]]=len[G[b][j]][G[a][i]]=w;
            int tmpsize=G[a].size();//合并2个集合
            for(int i=0;i<G[b].size();++i)
                G[a].push_back(G[b][i]);
            for(int i=0;i<tmpsize;++i)
                G[b].push_back(G[a][i]);
        }
    }
    secmst=INF;
    for(int i=0;i<m;++i)//枚举次小生成树
        if(vedge[i].selected==false)
            secmst=min(secmst,mst+vedge[i].w-len[vedge[i].a][vedge[i].b]);
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int t,n,m,x,y,w;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        vedge.resize(m);
        for(int i=0;i<m;++i){
            scanf("%d%d%d",&x,&y,&w);
            vedge[i]={x,y,w,false};
        }
        kruskal(n,m);
        if(mst!=secmst)
            printf("%d\n",mst);
        else
            puts("Not Unique!");
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值