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

题目链接 https://vjudge.net/problem/POJ-1679

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!

【题意】
给定一张无向图,判断这个图的最小生成树是否唯一,如果唯一则输出最小生成树的权值,否则输出 “Not Unique!”

【思路】
题目实际上想问的是次小生成树的权值,如果次小生成树的权值和最小生成树一样,那么最小生成树不唯一。最小生成树可以在求出每对点之间的最小瓶颈路后枚举剩下的边求出。具体做法是先求最小生成树,同时用dfs求出任意两点的最小瓶颈路,然后枚举所有不在最小生成树中的边,假设这条边的端点是u和v,用这条边去替换原来最小生成树中u,v的最小瓶颈路,迭代取最小值。所以次小生成树就是最小瓶颈路的一个简单应用。

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

const int inf = 0x3f3f3f3f;
const int maxn = 105;
const int maxm = 10050;

struct Edge {
    int from, to, dist;
    Edge(int f = 0, int t = 0, int d = 0) :from(f), to(t), dist(d) {}
    bool operator<(const Edge& e) const {
        return dist < e.dist;
    }
};

int n, m;
int par[maxn];
bool used[maxn];//dfs的访问标记数组
bool vis[maxm];//某条边是否使用过
int f[maxn][maxn];
vector<Edge> edges, g[maxn];

int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }

int kruscal() {
    int cnt = 0, ans = 0;
    for (int i = 0; i < n; ++i) {
        par[i] = i;
        g[i].clear();
    }
    for (int i = 0; i < m; ++i) {
        int u = edges[i].from;
        int v = edges[i].to;
        int x = find(u);
        int y = find(v);
        if (x != y) {
            par[x] = y;
            ans += edges[i].dist;
            vis[i] = true;
            g[u].push_back(Edge(u, v, edges[i].dist));
            g[v].push_back(Edge(v, u, edges[i].dist));
            if (++cnt == n - 1) break;
        }
    }
    return ans;
}

void dfs(int u) {
    used[u] = 1;
    for (int i = 0; i < g[u].size(); ++i) {
        int v = g[u][i].to;
        if (!used[v]) {
            for (int x = 0; x < n; ++x) {
                if (used[x]) f[x][v] = f[v][x] = max(f[x][u], g[u][i].dist);
            }
            dfs(v);
        }
    }
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &m);
        edges.clear();
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i < m; ++i) {
            int u, v, c;
            scanf("%d%d%d", &u, &v, &c);
            --u, --v;
            edges.push_back(Edge(u, v, c));
        }
        sort(edges.begin(), edges.end());
        int mst = kruscal();

        memset(used, 0, sizeof(used));
        memset(f, 0, sizeof(f));
        dfs(0);

        int ans = inf;
        //枚举所有没有用过的边
        for (int i = 0; i < m; ++i) {
            if (!vis[i]) {              
                ans = min(ans, mst - f[edges[i].from][edges[i].to] + edges[i].dist);
            }
        }

        if (ans == mst) printf("Not Unique!\n");
        else printf("%d\n", mst);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值