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

来源:互联网 发布:安全数据库 编辑:程序博客网 时间:2024/06/05 21:04


The Unique MST
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 30082 Accepted: 10769

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

23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2

Sample Output

3Not Unique!

题目翻译:

给出一个连通的无向图,给出N个节点(节点编号从1~N),M条边。判断这个图的最小生成树是否唯一。如果唯一输出最小生成树的值,否则输出Not Unique!

解题思路:

求其次小生成树。

次小生成树:就是生成树中权值第二小的树(其权值可以等于最小生成树),等于时说明最小生成树不唯一。

要求次小生成树,我们可以先求出最小生成树T,在构成最小生成树的边外选择一条边u,v,那么现在就有N条边了,此图必定存在环,因此我们去掉节点 u 到节点 v

在最小生成树的路径中最大的那条边,使它重新变成N-1条边,这样可以尽可能使新的生成树的值尽量小。


#include<iostream>#include<stdio.h>#include<math.h>#include<vector>#include<string.h>#include<algorithm>#define PI 3.1415926#define inf 0x3f3f3f3fusing namespace std;const int maxn = 102;int n,m;                   ///n个节点m条边int maxcost[maxn][maxn];   ///maxcost[i][j]存放在最小生成树中,节点i到节点j的路径中的最大权值bool mark[maxn][maxn];     ///用来标记那些边加入了最小生成树bool vis[maxn];            ///用来标记那些节点加入已经加入最小生成树。int lowcost[maxn];         ///lowcost[i]存放与i相连的点的最小距离int Map[maxn][maxn];       ///存放地图int closest[maxn];         ///lowcost[i]存放距离i最近的点。void InitMap()             ///图的初始化工作{    for(int i = 1; i <= n; i++)    {        Map[i][i] = 0;        for(int j = i+1; j <= n; j++)        {            Map[i][j] = Map[j][i] = inf;        }    }}long long int Prim(){    int Min,u;    memset(maxcost,0,sizeof(maxcost));    for(int i = 1; i <= n; i++)    {        lowcost[i] = Map[1][i];        closest[i] = 1;        vis[i] = 0;    }    vis[1] = 1;    for(int i = 1; i < n; i++)    {        Min = inf;        u = 0;        for(int j = 1; j <= n; j++)        {            if(vis[j]==0 && lowcost[j]<Min)            {                Min = lowcost[j];                u = j;            }        }        if(u == 0)            return -1;        vis[u] = 1;        int pre = closest[u];        ///在最小生成树中,u-pre只有两者一条边,权为Min        maxcost[u][pre] = maxcost[pre][u] = Min;        mark[u][pre] = mark[pre][u] = false;          ///代表这条边是最小生成树的边,标记为false        for(int j = 1; j <= n; j++)        {            maxcost[j][u] = max(maxcost[j][pre],maxcost[pre][u]);        }        for(int j = 1; j <= n; j++)        {            if(vis[j]==0)            {                if(Map[u][j]<lowcost[j])                {                    lowcost[j] = Map[u][j];                    closest[j] = u;                }            }        }    }    long long MST = 0;    for(int i = 1; i <= n; i++)    {        MST += lowcost[i];    }    return MST;}int main(){    int t;    int x,y,w;    cin>>t;    while(t--)    {        scanf("%d%d",&n,&m);         ///n个节点,m条边        memset(mark,false,sizeof(mark));        InitMap();                   ///初始化地图        for(int i = 1; i <= m; i++)        {            scanf("%d%d%d",&x,&y,&w);            Map[x][y] = Map[y][x] = w;            mark[x][y] = mark[y][x] = true;        }        long long int MST = Prim();        double temp = MST;        bool Unique = true;  ///假设最小生成树是唯一        for(int i = 1; i <= n; i++)        {            for(int j = 1; j <= n; j++)            {                if(i == j) continue;                /**不是构成最小生成树的边,加入最小生成树,                并减去节点i-j在最小生成树中最大的边**/                if(mark[i][j])                 {                    long long newmst = temp+Map[i][j]-maxcost[i][j];                    if(newmst == MST)                    {                        Unique = false;                        break;                    }                }            }            if(!Unique) break;        }        if(Unique) printf("%lld\n",MST);        else printf("Not Unique!\n");    }    return 0;}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小兔子不吃东西 精神不好怎么办 兔子要生宝宝了怎么办 宝宝让兔子咬了怎么办 兔子的右耳坏了怎么办 被小兔子咬了怎么办 打老鼠被老鼠咬了了怎么办 制作棒棒糖时木棍翘起怎么办 兔子被打后怕我怎么办 兔子后腿摔断了怎么办 兔子腿骨头断了怎么办 减肥老是控制不住吃怎么办 不爱吃主食爱吃零食怎么办 猫咪奶涨的很硬怎么办 斩魂技能乱加的怎么办 狗狗不吃东西还拉稀怎么办 狗狗拉稀不吃饭怎么办 狗狗几天不吃东西怎么办 狗狗不吃不喝怎么办 拖鞋洗了有臭味怎么办 拖鞋湿水了很臭怎么办 棉被上渗了酸奶怎么办 酸奶弄在衣服上怎么办 孩子把褥子尿湿怎么办 小孩尿过的褥子怎么办 包包放着发霉了怎么办 被套盖久了发黄怎么办 小白鞋里面发霉怎么办 棉被淋了一点雨怎么办 苹果手机被拉进电话群怎么办 墙上的插座烧了怎么办 富士康早退4分钟怎么办 我老婆起泡疹腿剌痛睡不着了怎么办 oppo耳机孔坏了怎么办 魅族耳机口坏了怎么办 控制线的报验资料怎么办 人失踪报警派出所不管怎么办 铁板的货架久了怎么办 干镀锌让环保查了怎么办 水管软管生锈了拧不动怎么办 镀锌管会生锈吗.怎么办 冷镀锌钢管生锈了怎么办