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

来源:互联网 发布:java 线程池 如何使用 编辑:程序博客网 时间:2024/05/21 09:55
The Unique MSTTime Limit: 1000MS      Memory Limit: 10000KTotal Submissions: 28409        Accepted: 10153DescriptionGiven 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'.InputThe 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.OutputFor each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.Sample Input23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2Sample Output3Not Unique!SourcePOJ 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 1
原创粉丝点击