POJ 1679 The unique MST [次小生成树]

The Unique MST
Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %lld & %llu

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


一般的次小生成树。。
这道题数据范围不需要用倍增,那么就暴力替换就可以了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=105;
struct Edge
{
    int from,to;
    int val;
    bool operator < (const Edge &t)const
    {
        return val<t.val;
    }
}edge[maxn*maxn];
struct MST
{
    int to,next;
    int val;
}mst[maxn<<1];
int head[maxn];
int maxedge;
int n,m,fa[maxn];
bool inMST[maxn*maxn],vis[maxn];
int maxcost[maxn][maxn];
inline void addmst(int u,int v,int c)
{
    mst[++maxedge]=(MST){v,head[u],c};
    head[u]=maxedge;
    mst[++maxedge]=(MST){u,head[v],c};
    head[v]=maxedge;
}
inline void init()
{
    memset(head,-1,sizeof(head));
    memset(inMST,0,sizeof(inMST));
    memset(maxcost,0,sizeof(maxcost));
    maxedge=-1;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
        scanf("%d%d%d",&edge[i].from,&edge[i].to,&edge[i].val);
    for(int i=1;i<=n;i++) fa[i]=i;
}
inline int find(int x)
{
    if(fa[x]!=x) fa[x]=find(fa[x]);
    return fa[x];
}
inline bool union_find(int x,int y)
{
    int t1=find(x),t2=find(y);
    if(t1==t2) return false;
    fa[t2]=t1;
    return true;
}
inline int kruskal()
{
    int ret=0;
    sort(edge+1,edge+m+1);
    int tot=0,pos=1;
    while(tot!=n-1)
    {
        if(union_find(edge[pos].from,edge[pos].to))
        {
            tot++,ret+=edge[pos].val;
            addmst(edge[pos].from,edge[pos].to,edge[pos].val);
            inMST[pos]=true;
        }
        if(++pos>m) break;//do not return herte in case the last edge to be NO.m
    }
    if(tot^(n-1)) return -1;
    else return ret;
}
void dfs(int s,int t,int val)
{
    vis[t]=true;
    maxcost[s][t]=val;
    for(int i=head[t];~i;i=mst[i].next)
    {
        int v=mst[i].to;
        if(vis[v]) continue;
        dfs(s,v,max(val,mst[i].val));
    }
}
inline int get_maxcost(int x,int y)
{
    if(!maxcost[x][y]) return maxcost[y][x];
    return maxcost[x][y];
}
int main()
{
    #ifdef Local
    freopen("unique.in","r",stdin);
    freopen("unique.out","w",stdout);
    #endif
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        init();
        int minMST=kruskal(),subMST=INF;
        for(int i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            dfs(i,i,0);
        }
        for(int i=1;i<=m;i++)
            if(!inMST[i]) subMST=min(subMST,minMST-get_maxcost(edge[i].from,edge[i].to)+edge[i].val);
        if(minMST^subMST) printf("%d\n",minMST);
        else printf("Not Unique!\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值