首页 > 代码库 > (次小生成树) poj 1679

(次小生成树) poj 1679

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 21358 Accepted: 7560

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!

Source

POJ Monthly--2004.06.27 srbga@POJ
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<string>#include<cmath>#include<algorithm>using namespace std;#define INF 1<<30int n,m,a[110][110],low[110],vis[110],pre[110];int maxx[110][110],connect[110][110],mst;int prime(){      int minn,pos,fa,ans=0;      vis[1]=1,pos=1;      for(int i=1;i<=n;i++)            if(i!=pos)            {                  low[i]=a[pos][i];                  pre[i]=pos;            }      for(int i=1;i<n;i++)      {            minn=INF;            for(int j=1;j<=n;j++)            {                  if(!vis[j]&&minn>low[j])                  {                        pos=j;                        minn=low[j];                  }            }            fa=pre[pos];            ans+=minn;            vis[pos]=1;            connect[fa][pos]=connect[pos][fa]=1;            maxx[fa][pos]=maxx[pos][fa]=minn;            for(int j=1;j<=n;j++)                  maxx[pos][j]=maxx[j][pos]=max(maxx[pos][fa],maxx[fa][j]);            for(int j=1;j<=n;j++)            {                  if(!vis[j]&&low[j]>a[pos][j])                  {                        low[j]=a[pos][j];                        pre[j]=pos;                  }            }      }      return ans;}int main(){      int tt;      scanf("%d",&tt);      while(tt--)      {            bool flag=false;            memset(vis,0,sizeof(vis));            memset(connect,0,sizeof(connect));            memset(maxx,0,sizeof(maxx));            scanf("%d%d",&n,&m);            for(int i=1;i<=n;i++)                  for(int j=1;j<=n;j++)                        a[i][j]=INF;            for(int i=1;i<=m;i++)            {                  int x,y,z;                  scanf("%d%d%d",&x,&y,&z);                  a[x][y]=z,a[y][x]=z;            }            mst=prime();            for(int i=1;i<=n;i++)                  for(int j=1;j<=n;j++)                  {                        if(connect[i][j]||a[i][j]==INF)                              continue;                        if(a[i][j]==maxx[i][j])                        {                              flag=true;                              break;                        }                  }            if(flag)                  printf("Not Unique!\n");            else                  printf("%d\n",mst);      }      return 0;}

  

(次小生成树) poj 1679