POJ 1679 The Unique MST(次小生成树)

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 31405 Accepted: 11318

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


题意:T组测试数据,n个点,m条边,判断这个图最小生成树是否唯一即次小生成树权值是否等于最小生成树权值。

分析:一个图的最小生成树我们可以用Prim或者Kruskal来计算,那么次小生成树就是在最小生成树T的基础上,去掉T中的某条边,然后再补上T之外一条边,重新构成一个生成树,并且求出这些新生成树的最小值。这里我的具体操作是,使用Prim计算最小生成树T,用邻接表存取T中边的信息,同时记录T中使用了哪些边。这些完成之后,我们就可求出MST的值。然后遍历所有的顶点ui,求出以ui为顶点的最小生成树中的最大边v,然后将该边去掉,连接edge[u][v],这时又构成一个生成树,我们只要不断更新新生成树的最小权值就行了。时间复杂度应该是是O(n*n)。因为我在查找最大边的时候时间是O(m),m=n-1。

参考代码:
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

struct Edge{
    int u;
    int v;
    int w;
    int next;
};

Edge edge[100005];
int head[105];
int cnt;
int cost[105][105];
int n,m;
int sign[105][105];

void add(int u,int v,int w){
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int MAX[105];
int MST;
int pre[105];

void prim(){
    int id[105];
    int d[105];
    for(int i=1;i<=n;i++){
        id[i]=0;
        d[i]=cost[1][i];
        pre[i]=1;
    }
    d[1]=0;
    id[1]=1;
    int cur=1;
    for(int i=1;i<n;i++){
        int pos=1;
        int Min=0x3f3f3f3f;
        for(int j=1;j<=n;j++){
            if(d[j]<Min&&id[j]==0){
                Min=d[j];
                pos=j;
            }
        }
        add(pre[pos],pos,Min);
        add(pos,pre[pos],Min);
        sign[pre[pos]][pos]=1;
        sign[pos][pre[pos]]=1;
        id[pos]=1;
        MST+=Min;
        for(int j=1;j<=n;j++){
            if(d[j]>cost[pos][j]&&id[j]==0){
                d[j]=cost[pos][j];
                pre[j]=pos;
            }
        }
    }
}

void DFS(int u,int pre){
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].v;
        if(v!=pre){
            int w=edge[i].w;
            MAX[v]=max(MAX[v],w);
            DFS(v,u);
        }
    }
}

int main(){
    int N;
    scanf("%d",&N);
    while(N--){
        cnt=0;
        scanf("%d%d",&n,&m);
        memset(head,-1,sizeof(head));
        memset(cost,0x3f3f3f3f,sizeof(cost));
        memset(sign,0,sizeof(sign));
        for(int i=1;i<=m;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            cost[u][v]=cost[v][u]=w;
        }
        MST=0;
        prim();
        int ans=0x3f3f3f3f;
        for(int i=1;i<=n;i++){
            memset(MAX,0,sizeof(MAX));
            DFS(i,-1);
            for(int j=1;j<=n;j++){
                if(sign[i][j]==0){
                    ans=min(ans,MST-MAX[j]+cost[i][j]);
                }
            }
        }
        if(ans!=MST){
            printf("%d\n",MST);
        }
        else{
            printf("Not Unique!\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值