POJ1679(次小生成树)

The Unique MST

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24201   Accepted: 8596

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!题意:判断是否存在唯一的最小生成树。思路:先求出一个最小生成树。在这颗树上先加入(x,y),加入后一定会成环,如果删除(x,y)以外最大的一条边,会得到加入(x,y)时权值最小的一棵树。如果加入的边和删除的边相等,则最小生成不是唯一的。收获:了解了次小生成树。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std;

const int INF=0x3f3f3f3f;
const double eps=1e-10;
const double PI=acos(-1.0);
#define maxn 500
int n, m;
int a[maxn][maxn];
int used[maxn][maxn];
int mmax[maxn][maxn];
int vis[maxn];
int dis[maxn];
int pre[maxn];
int prim()
{
    int ans = 0;
    memset(vis, 0, sizeof vis);
    memset(used, 0, sizeof used);
    memset(mmax, 0 ,sizeof mmax);
    for(int i = 2; i <= n; i++)
    {
        dis[i] = a[1][i];
        pre[i] = 1;
    }
    vis[1] = 1;
    dis[1] = 0;
    for(int i = 1; i < n; i++)
    {
        int temp = INF;
        int k = -1;
        for(int j = 1; j <= n; j++)
        {
            if(!vis[j] && temp > dis[j])
            {
                temp = dis[j];
                k = j;
            }
        }
        if(k == -1) return ans;
        ans += temp;
        vis[k] = 1;
        used[k][pre[k]] = used[pre[k]][k] = 1;
        mmax[pre[k]][k] = temp;
        for(int j = 1; j <= n; j++)
        mmax[j][k] = max(mmax[j][pre[k]],mmax[pre[k]][k] );//找最大的边权的边。
        for(int j = 1; j <= n; j++)
        {
            if(!vis[j] && dis[j] > a[k][j])
            {
                dis[j] = a[k][j];
                pre[j] = k;
            }
        }
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &n, &m);
        int u, v, w;
        memset(a, INF, sizeof a);
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &u, &v, &w);
            a[u][v] = a[v][u] = w;
        }
        int mst = prim();
        int flag = 0;
        for(int i = 1; i <= n; i++)
            for(int j = i+1; j <= n; j++)
            {
                if(a[i][j] == INF || used[i][j] == 1)
                    continue;
                if(a[i][j] == mmax[i][j])//判断加入的边和删除的边是否相等。
                {
                    flag = 1;
                    break;
                }
            }
        if(flag)
        printf("Not Unique!\n");
        else
        printf("%d\n",mst);
    }
    return 0;
}
时间: 2024-10-08 23:47:42

POJ1679(次小生成树)的相关文章

poj1679+次小生成树

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

poj1679次小生成树入门题

次小生成树求法:例如求最小生成树用到了 1.2.4这三条边,总共5条边,那循环3次的时候,每次分别不用1.2.4求得最小生成树的MST,最小的MST即为次小生成树 如下代码maxx即每次删去1,2,4边之后求得的最大边 #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstd

POJ1679 The Unique MST 【次小生成树】

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

次小生成树(POJ1679/CDOJ1959)

POJ1679 首先求出最小生成树,记录权值之和为MinST.然后枚举添加边(u,v),加上后必形成一个环,找到环上非(u,v)边的权值最大的边,把它删除,计算当前生成树的权值之和,取所有枚举加边后生成树权值之和的最小值 思路: 最小生成树唯一性判断,求次小生成树的val再与最小生成树比较.1.先用prim算法求出最小生成树.并在其过程中保存加入到MST中的Max[i][j]( i 到 j 路径中的最大边 ) 2.对未加入MST的边进行遍历:从MST中去掉i j路径中的最大边,加入未访问边G[i

[POJ1679]The Unique MST 次小生成树

题目链接:http://poj.org/problem?id=1679 给你一个图的连通情况,询问你此图的最小生成树是否唯一. 假如最小生成树唯一,即生成树连通所有节点的权值和唯一.假如不唯一,那么存在另一条最小生成树使得权值等于之前最小生成树的权值. 换个思路考虑,也就是次小生成树的权值与最小生成树的权值相同,那么问题就变成了求次小生成树的权值. 我选择的是先求出最小生成树,将树上用到的边都保存下来.接着分别将每一条用到的边摘下来,再求一次最小生成树.假如不包含当前删掉的边生成的生成树的所选边

POJ1679 The Unique MST【Kruskal】【次小生成树】

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

poj1679——The Unique MST(次小生成树,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 followin

次小生成树

次小生成树:所谓的次小生成树,是指在边集与某一最小生成树的边集不完全相同的其它生成树中值最小的那个.因此在数值上,最小生成树可能会等于次小生成树,这种情况也可以看做最小生成数. 思路:最直观的解法是,首先求出最小生成树,并且记录最小生成树中的边,然后再枚举删除最小生成树的边并同时求最小生成树. 以POJ1679为例,Kruskal求次小生成树代码如下: 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm>

次小生成树的模版

求次小生成树的步骤是: 1.求出最小生成树MST,用一个矩阵maxe[u][v]记录在MST中连接u-v的路径中权值最大的边. 2.枚举所有不在T中的边u-v,加入边u-v,删除权值为max[u][v]的边,不断枚举找到次小生成树. 由于邻接矩阵建图无法储存平行边(重边),我们在建图的时候都是取当前最小值.所以用prim做的话,很可能出错.但是prim也是有市场的,当没有平行边,并且定点数比较少的时候,用prim就有优势.如果有平行边,就必须用kruskal.用链式前向星建图. 下面是两个模版,