[POJ 1679] The Unique MST

The Unique MST
 

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

POJ Monthly--2004.06.27 srbga@POJ
 
次小生成树模板、、、
#include <iostream>

#include <cstdio>

#include <cstring>

using namespace std;

#define INF 0x3f3f3f3f

#define N 110



int n,m;

int closest[N];

int lowcost[N];

int mpt[N][N];

int mxd[N][N];

bool vis[N];

bool connect[N][N];

int ans1,ans2;



void prim(int s)

{

    int fk,k,i,j,minc;

    for(i=1;i<=n;i++)

    {

        closest[i]=s;

        lowcost[i]=mpt[s][i];

    }

    vis[s]=1;

    for(i=1;i<n;i++)

    {

        minc=INF;

        for(j=1;j<=n;j++)

        {

            if(!vis[j] && lowcost[j]<minc)

            {

                k=j;

                minc=lowcost[j];

            }

        }

        if(minc==INF) break;

        vis[k]=1;

        ans1+=minc;

        fk=closest[k];

        connect[fk][k]=1;

        connect[k][fk]=1;

        for(j=1;j<=n;j++)

        {

            if(vis[j] && j!=k) mxd[k][j]=mxd[j][k]=max(mxd[j][fk],lowcost[k]);

            if(!vis[j] && mpt[k][j]<lowcost[j])

            {

                lowcost[j]=mpt[k][j];

                closest[j]=k;

            }

        }

    }

}



int main()

{

    int T;

    scanf("%d",&T);

    while(T--)

    {

        scanf("%d%d",&n,&m);

        ans1=0;

        ans2=INF;

        memset(mxd,0,sizeof(mxd));

        memset(mpt,INF,sizeof(mpt));

        memset(vis,0,sizeof(vis));

        memset(connect,0,sizeof(connect));



        while(m--)

        {

            int a,b,c;

            scanf("%d%d%d",&a,&b,&c);

            mpt[a][b]=c;

            mpt[b][a]=c;

        }

        prim(1);

        for(int i=1;i<=n;i++)

        {

            for(int j=1;j<=n;j++)

            {

                if(!connect[i][j])

                {

                    ans2=min(ans2,ans1-mxd[i][j]+mpt[i][j]);

                }

            }

        }

        if(ans1!=ans2)

            printf("%d\n",ans1);

        else

            printf("Not Unique!\n");

    }

    return 0;

}

 

你可能感兴趣的:(unique)