次小生成树

Posted zzyh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了次小生成树相关的知识,希望对你有一定的参考价值。

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 30194   Accepted: 10809

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!

Source

【解析】
先生成最小生成树,再枚举删去每一条边,看看看生成的次小生成树与最小生成树是否相等。
【代码】
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 101
struct Edge
{
    int x,y,z;
}edge[2*N];
int t,x,y,flag,ans,ans2,n,m,num2;
int far[N];
bool path[2*N];
void set()
{
    for(int i=1;i<=n;i++)
    far[i]=i;
}
void unionn(int x,int y)
{
    far[x]=y;
}
int f(int x)
{
    far[x]==x?x:far[x]=f(far[x]);
}
bool cmp(Edge a,Edge b)
{
    return a.x<b.y;
}
void kruskal()
{
    set();
    int num1=0;
    sort(edge+1,edge+m+1,cmp);
    for(int i=1;i<=m;i++)
    {
        int fx=f(edge[i].x),fy=f(edge[i].y);
        if(fx!=fy)
        {
            unionn(fx,fy);
            ans+=edge[i].z;
            path[++num1]=i;
        }
        if(num1==n-1)
        break;
    }
    
    for(int k=1;k<=num1;k++)
    {
    set();
        ans2=0;num2=0;
        for(int i=1;i<=m;i++)
        {
            if(i==path[i])
            continue;
            int fx=f(edge[i].x),fy=f(edge[i].y);
            if(fx!=fy)
            {
                unionn(fx,fy);
                ans2+=edge[i].z;
                num2++;
            }
            if(num2==n-1&&ans2==ans)flag=1;
        }
    }
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].z);
    flag=0;
    ans=0;
    kruskal();
    if(flag)
    printf("Not Unique!
");
    else
    printf("%d
",ans);
    }
    return 0;
}

 

以上是关于次小生成树的主要内容,如果未能解决你的问题,请参考以下文章

次小生成树

次小生成树

poj1679次小生成树入门题

次小生成树

严格次小生成树(Bzoj1977:[Beijing2010组队]次小生成树)

次小生成树