POJ1679 The Unique MST(次小生成树[Kruskal]+链式前向星or普通版本)

The Unique MST
Time Limit: 1000MS      Memory Limit: 10000K
Total Submissions: 28825        Accepted: 10293
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!
最近不知道为什么,心态容易爆炸,这道题目re了超级多次,差点砸电脑,明明就是模板题来着。
题意就是MST是否唯一,即有没有相同的值的最小生成树,
明显求次小生成数,若值相同,则MST唯一。
链式前向星版本:
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define inf 0x3f3f3f3f
#define Pi 4.0*atan(1.0)

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 110;
using namespace std;
inline int read(){
    int x(0),f(1);
    char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
struct Edge{
    int u,v,w;
    bool vis;
    friend bool operator<(const Edge &a,const Edge &b){
        if(a.w!=b.w)return a.w<b.w;
        if(a.u!=b.u)return a.u<b.u;
        return a.w<b.w;
    }
};
struct node{
    int to,next,w;
};
/*
 * 次小生成树
 * 求最小生成树时,用数组len[i][j]来表示MST中i到j最大边权
 * 求完后,直接枚举所有不在MST中的边,替换掉最大边权的边,更新答案
 * 点的编号从1开始
*/
int secmst,mst;
int head[maxn];
bool vis[maxn];
int len[maxn][maxn]; //len[i][j]表示在最小生成树中从i到j的路径中的最大边权
int e[maxn];
int par[maxn];
vector<Edge> edge;
//node G[maxn];
node G[maxn];
void init()
{
    mes(head,-1);
    mes(e,-1);
}
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);
}
void kruskal(int n,int m)
{
    for(int i=1;i<=n;++i){
        par[i]=i;
    }
    for(int i=1;i<=n;++i){
        G[i].to=i;
        G[i].next=head[i];
        e[i]=i;
        head[i]=i;
    }
    int ed=0;
    mst=0;
    sort(edge.begin(),edge.end());
    for(int i=0;i<m;++i){
        if(ed==n-1){
            break;
        }
        int x=edge[i].u;
        int y=edge[i].v;
        int w=edge[i].w;
        x=find(x),y=find(y);
        if(x!=y){
            mst+=w;
            for(int k=head[x];~k;k=G[k].next){
                for(int j=head[y];~j;j=G[j].next){
                    len[G[k].to][G[j].to]=len[G[j].to][G[k].to]=w;
                }
            }
            G[e[y]].next=head[x];
            head[x]=head[y];
            e[y]=e[x];
            ++ed;
            merge(x,y);
            edge[i].vis=true;
        }
    }
    secmst=inf;
    for(int i=0;i<m;++i){
        if(edge[i].vis==false){
            secmst=min(secmst,mst+edge[i].w-len[edge[i].u][edge[i].v]);
        }
    }
}
int main()
{
   // fin;
    int t,n,m,u,v,w;
    t=read();
    while(t--){
        n=read(),m=read();
        mes(len,0x3f);
        init();
        edge.resize(m);
        for(int i=0;i<m;++i){
            u=read(),v=read(),w=read();
            edge[i].u=u;
            edge[i].v=v;
            edge[i].w=w;
            edge[i].vis=false;
        }
        kruskal(n,m);
        if(mst!=secmst){
            printf("%d\n",mst);
        }
        else{
            puts("Not Unique!");
        }
    }
    return 0;
}
普通版本:
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define inf 0x3f3f3f3f

#define Pi 4.0*atan(1.0)

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 1100;
using namespace std;
inline int read(){
    int x(0),f(1);
    char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
struct Edge{
    int u,v,w;
    bool vis;
    friend bool operator<(const Edge &a,const Edge &b){
        if(a.w!=b.w)return a.w<b.w;
        if(a.u!=b.u)return a.u<b.u;
        return a.w<b.w;
    }
};
struct node{
    int to,next,w;
};
int secmst,mst;
int head[maxn];
bool vis[maxn];
int len[maxn][maxn];
int e[maxn];
int par[maxn];
vector<Edge> edge;
//node G[maxn];
vector<int> G[maxn];
inline int find(int x)
{
    return x==par[x]?x:par[x]=find(par[x]);
}
void kruskal(int n,int m)
{
    for(int i=1;i<=n;++i){
        par[i]=i;
        G[i].clear();
        G[i].push_back(i);
    }
    int ed=0;
    mst=0;
    sort(edge.begin(),edge.end());
    for(int i=0;i<m;++i){
        if(ed==n-1){
            break;
        }
        int x=edge[i].u;
        int y=edge[i].v;
        int w=edge[i].w;
        x=find(x),y=find(y);
        if(x!=y){
            mst+=w;
            ++ed;
            par[y]=x;
            edge[i].vis=true;
            for(int k=0;k<G[x].size();++k)
                for(int j=0;j<G[y].size();++j){
                    len[G[x][k]][G[y][j]]=len[G[y][j]][G[x][k]]=w;
                }
            int tmp=G[x].size();
            for(int j=0;j<G[y].size();++j){
                G[x].push_back(G[y][j]);
            }
            for(int j=0;j<tmp;++j)
                G[y].push_back(G[x][j]);
        }
    }
    secmst=inf;
    for(int i=0;i<m;++i){
        if(edge[i].vis==false){
            secmst=min(secmst,mst+edge[i].w-len[edge[i].u][edge[i].v]);
        }
    }
}
int main()
{
   // fin;
    int t,n,m,u,v,w;
    t=read();
    while(t--){
        n=read(),m=read();
        mes(len,0x3f);
        edge.resize(m);
        for(int i=0;i<m;++i){
            u=read(),v=read(),w=read();
            edge[i].u=u;
            edge[i].v=v;
            edge[i].w=w;
            edge[i].vis=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、付费专栏及课程。

余额充值