poj 1679 The Unique MST,次小生成树

次小生成树

求最小生成树时,用数组Max[i][j]来表示MST中i到j的最大边权。

求完后,直接枚举所有不在MST中的边,替换掉最大边权的边,更新答案。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 110;
const int INF = 1e9;
bool vis[maxn];
int d[maxn];
int pre[maxn];
int Max[maxn][maxn];
bool used[maxn][maxn];
int g[maxn][maxn];
int n, m;

int Prim()
{
    int ans = 0;
    memset(vis, false, sizeof vis );
    memset(Max, 0, sizeof Max );
    memset(used, false, sizeof used );
    vis[0] = true;
    pre[0] = -1;
    for(int i=1; i<n; ++i) {
        d[i] = g[0][i];
        pre[i] = 0;
    }
    for(int i=1; i<n; ++i) {
        int p = -1;
        for(int j=0; j<n; ++j) if(!vis[j] && (p==-1||d[p]>d[j]))
                p = j;
        if(-1==p) return -1;
        ans += d[p];
        vis[p] = true;
        used[p][pre[p]] = used[pre[p]][p] = true;
        for(int j=0; j<n; ++j) {
            if(vis[j]) Max[j][p] = Max[p][j] = max(Max[j][pre[p]], d[p]);
            if(!vis[j]&&d[j]>g[p][j]) {
                d[j] = g[p][j];
                pre[j] = p;
            }
        }
    }
    return ans;
}

void solve()
{
    int res = Prim();
    if(-1 == res) { //图不连通
        puts("Not Unique!");
        return ;
    }
    int Mn = INF;
    for(int i=0; i<n; ++i)
        for(int j=i+1; j<n; ++j)
            if(g[i][j]!=INF && !used[i][j]) {
                Mn = min(Mn, g[i][j]-Max[i][j]);
            }
    if(Mn==INF || Mn!=0){ //不存在次小生成树 或 不相等
        printf("%d\n", res);
    }else {
        puts("Not Unique!");
    }
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &n, &m);
        for(int i=0; i<n; ++i) for(int j=0; j<n; ++j) g[i][j] = INF;
        int x, y, z;
        while(m--) {
            scanf("%d%d%d", &x, &y, &z);
            x--;
            y--;
            g[x][y] = g[y][x] = z;
        }
        solve();
    }
    return 0;
}
时间: 2024-10-15 14:12:32

poj 1679 The Unique MST,次小生成树的相关文章

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

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20293   Accepted: 7124 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35999   Accepted: 13145 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undir

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

版权声明:本文为博主原创文章,未经博主允许不得转载.

hdu 1679 The Unique MST 次小生成树 简单题

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21737   Accepted: 7692 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

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

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 followin

poj 1679 The Unique MST 【次小生成树】【模板】

题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后添加到最小生成树上,然后把原树上添加了之后形成环的最长的边删去,知道一个最小的.就是次小生成树. 这些需要的都可以在求解最小生成树的时候处理出来. AC代码: #include <cstdio> #include <cstring> #include <iostream> #i

poj 1679 The Unique MST (判断最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20679   Accepted: 7255 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1679 The Unique MST(求最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20430   Accepted: 7186 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1679 The Unique MST 【最小生成树/次小生成树】

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22668   Accepted: 8038 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

poj 1679 The Unique MST (判定最小生成树是否唯一)

题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29408   Accepted: 10520 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spannin