POJ1679(次小生成树)

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 27651   Accepted: 9909

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!

求次小生成树步骤:
1)用prime求最小生成树,并记录树中u,v结点路径之间边的权值的最大值dp[u][v]。
2)一次枚举u,v结点不在最小生成树中的边,并将dp[u][v]删除,形成新的树。记录形成所有新树的权值的最小值。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 105;
const int INF = 0x3f3f3f3f;
int n, m, arc[MAXN][MAXN];
int d[MAXN], vis[MAXN], mst[MAXN][MAXN], dp[MAXN][MAXN], pre[MAXN];
int first, second;
void prim(int src)
{
    for(int i = 1; i <= n; i++)
    {
        d[i] = INF;
        vis[i] = 0;
        pre[i] = -1;
        for(int j = 1; j <= n; j++)
        {
            mst[i][j] = 0;
            dp[i][j] = 0;
        }
    }
    d[src] = 0;
    int t = n;
    while(t--)
    {
        int mincost = INF, k;
        for(int i = 1; i <= n; i++)
        {
            if(!vis[i] && d[i] < mincost)
            {
                mincost = d[i];
                k = i;
            }
        }
        first += mincost;
        int fa = pre[k];
        if(fa != -1)
        {
            mst[fa][k] = mst[k][fa] = 1;
        }
        for(int i = 1; i <= n; i++)
        {
            if(vis[i])
            {
                dp[i][k] = dp[k][i] = max(dp[i][fa], mincost);
            }
        }
        vis[k] = 1;
        for(int i = 1; i <= n; i++)
        {
            if(!vis[i] && d[i] > arc[i][k])
            {
                d[i] = arc[i][k];
                pre[i] = k;
            }
        }
    }
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        first = 0;
        scanf("%d %d", &n, &m);
        for(int i = 1; i <= n ; i++)
            for(int j = 1; j <= n; j++) arc[i][j] = INF;
        for(int i = 0 ; i < m; i++)
        {
            int u, v, w;
            scanf("%d %d %d", &u, &v, &w);
            arc[u][v] = arc[v][u] = min(arc[u][v], w);
        }
        prim(1);
        second = INF;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(arc[i][j] != INF && !mst[i][j])
                {
                    second = min(second, first - dp[i][j] + arc[i][j]);
                }
            }
        }
        if(first == second)
        {
            printf("Not Unique!
");
        }
        else
        {
            printf("%d
", first);
        }
    }
    return 0;
}

 方法2:

/*
    1.采用kruskal算法求最小生成树并记录下最小生成树上的边
    2.将原图中最小生成树上的边依次删除分别再用prim算法求最小生成树(注意:删边后图可能不连通)
    3.比较删边前后的最小生成树是否相同
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>

using namespace std;

const int INF = 0x3f3f3f3f;

struct Edge {
    int u, v, w;
}es[10005];

bool comp(Edge e1, Edge e2) {
    return e1.w < e2.w;
}

int n, m;

int par[105];

void prep(int n) {
    for(int i = 1; i <= n; i++) {
        par[i] = i;
    }
}
int fnd(int x) {
    if(par[x] == x) {
        return x;
    }
    return par[x] = fnd(par[x]);
}
void unite(int a, int b) {
    int fa = fnd(a);
    int fb = fnd(b);
    par[fa] = fb;
}
bool same(int a, int b) {
    return fnd(a) == fnd(b);
}

struct Arc {
    int to, w;
    Arc(int to,int w) {
        this->to = to;
        this->w = w;
    }
    bool operator<(const Arc &arc) const{
        return w > arc.w;
    }
};
vector<Arc> arc[105];
bool vis[105];
int d[105];
int prim(pair<int, int> usedE) {
    for(int i = 1; i <= n; i++) {
        vis[i] = false;
        d[i] = INF;
    }
    priority_queue<Arc> pque;
    pque.push(Arc(1, 0));
    int mincost = 0;
    while(!pque.empty()) {
        Arc nod = pque.top(); pque.pop();
        if(vis[nod.to]) {
            continue;
        }
        mincost += nod.w;
        vis[nod.to] = true;
        for(int i = 0; i < arc[nod.to].size(); i++) {
            Arc e = arc[nod.to][i];
            if((usedE.first == e.to && usedE.second == nod.to) || (usedE.first == nod.to && usedE.second == e.to)) {
                continue;
            }
            if(d[e.to] > e.w) {
                d[e.to] = e.w;
                pque.push(Arc(e.to, e.w));
            }
        }
    }
    for(int i = 1; i <= n; i++) {
        if(!vis[i]) {
            return -1;  //删边后图不连通
        }
    }
    return mincost;
}

int main() {
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while(T--) {
        cin >> n >> m;
        for(int i = 1; i <= n; i++) {
            arc[i].clear();
        }
        for(int i = 0; i < m; i++) {
            cin >> es[i].u >> es[i].v >> es[i].w;
            arc[es[i].u].push_back(Arc(es[i].v, es[i].w));
            arc[es[i].v].push_back(Arc(es[i].u, es[i].w));
        }
        prep(n);
        sort(es, es+m, comp);
        int mstCost = 0;
        vector<pair<int, int> > mstEs;
        for(int i = 0; i < m; i++) {
            if(!same(es[i].u, es[i].v)) {
                mstCost += es[i].w;
                mstEs.push_back(pair<int,int>(es[i].u, es[i].v));
                unite(es[i].u, es[i].v);
            }
        }
        bool tag = true;
        for(int i = 0; i < mstEs.size(); i++) {
            int t = prim(mstEs[i]);
            if(t == -1) {
                continue;
            }
            if(t == mstCost) {
                tag = false;
                break;
            }
        }
        if(tag) {
            cout<< mstCost << endl;
        } else {
            cout << "Not Unique!" << endl;
        }
    }

    return 0;
}
原文地址:https://www.cnblogs.com/program-ccc/p/5816035.html