POJ 1679 Kruskal(最小生成树+次小生成树)Kruskal

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!

题意:给你一个n个节点m条边的无向图,问你该图的最小生成树是否唯一?如果唯一输出,树的权值,否则输出'Not Unique!'.

思路:判断最小生成树的总权值是否等于次小生成树的权值。

求次小生成树方法:一个图的次小生成树(权值<=最小生成树的权值,可能等于最小生成树的权值)肯定至少有一条边与最小生成树的一条边不同.所以我们可以枚举最小生成树上的边,然后依次用m-1条边的无向图来重新生成最小生成树,求出新生成的最小生成树中的权值最小值即为次小生成树的权值.   求出最小生成树,(这里在添加边的时候,多加一项纪录每条边的标记)然后枚举每条边,依次用总的边数减去最小生成树的每条边 用剩下的总边重新依次找最小生成树和用总边数找的最小生成树比较。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=100+10;
const int maxm=100*100+10;
 
struct Edge
{
    int u,v,dist;
    int id;//原始编号
    Edge(){}
    Edge(int u,int v,int d,int id):u(u),v(v),dist(d),id(id){}
    bool operator<(const Edge &rhs)const
    {
        return dist <rhs.dist;
    }
};
 
struct Kruskal
{
    int n,m;
    Edge edges[maxm];
    vector<int> E;//保存最小生成树上的边原始序号
    int fa[maxn];//并查集相关
    int findset(int x){ return fa[x]==-1? x: fa[x]=findset(fa[x]); }
 
    void init(int n)
    {
        this->n=n;
        m=0;
    }
 
    void AddEdge(int u,int v,int dist,int id)
    {
        edges[m++]=Edge(u,v,dist,id);  //这里的id就是每条边的标记。用于找次小生成树时依次删除。
    }
 
    int kruskal(int ID)
    {
        E.clear();
        memset(fa,-1,sizeof(fa));
        int sum=0;  //最小生成树权值
        int cnt=0;  //最小生成树边数目
        sort(edges,edges+m);
 
        for(int i=0;i<m;i++)
        {
            if(edges[i].id == ID) continue;//ID边被删除
            int u=edges[i].u, v=edges[i].v;
            if(findset(u) != findset(v))
            {
                E.push_back(edges[i].id);
                fa[findset(u)] = findset(v);
                sum +=edges[i].dist;
                if(++cnt>=n-1) break;
            }
        }
        if(cnt<n-1) return -1;
        return sum;
    }
}KK;
 
int main()
{
    int T; scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        KK.init(n);
        for(int i=0;i<m;i++)
        {
            int u,v,d;
            scanf("%d%d%d",&u,&v,&d);
            KK.AddEdge(u,v,d,i);
        }
        int ans1 = KK.kruskal(-1);
        int ans2 = 1e9;
 
        vector<int> E(KK.E);    //保存原图最小生成树上的
        for(int i=0;i<E.size();i++)
        {
            int tmp = KK.kruskal(E[i]);
            if(tmp ==-1) continue; //此时图不连通,不存在最小生成树
            ans2 = min(ans2,tmp);
            if(ans2 == ans1) break;
        }
        if(ans1==ans2) printf("Not Unique!\n");
        else printf("%d\n",ans1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40859951/article/details/83757077
今日推荐