1. 程式人生 > >Poj 1679 The Unique MST 次小生成樹

Poj 1679 The Unique MST 次小生成樹

                                                                                               The Unique MST

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 36848 Accepted: 13426

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!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 1000 + 10;
const int maxm = 1e5 + 10;
const int INF = 0x3f3f3f3f;

int n,m;
int f[maxn];
int g[maxn][maxn];
bool in[maxn][maxn];
int md[maxn][maxn];

struct edge
{
  int u,v,c;
  edge(int u = 0, int v = 0, int c = 0) : u(u), v(v), c(c) {}
}es[maxm];


bool cmp(edge a, edge b)
{
  return  a.c < b.c;
}

void init()
{
  for(int i = 0; i <= n; i++) f[i] = i;
}

int find(int x)
{
   return x == f[x] ? x : f[x] = find(f[x]);
}

void unit(int x, int y)
{
  f[find(x)] = find(y);
}

bool same(int x, int y)
{
  return find(x) == find(y);
}

int vis[maxn];

void dfs(int s,int x, int dis)
{
   vis[x] = true;
   for(int i = 1; i <= n; i++)
    {
       if(g[x][i] != -1 && !vis[i] && in[x][i])
            {
             md[s][i] = max(dis,g[x][i]);  
             dfs(s,i,max(dis,g[x][i]));
             }
    }
}

int main()
{
  int T;
  scanf("%d",&T);
  while(T--)
  {
    scanf("%d%d",&n,&m);
    init();
    memset(g,-1,sizeof(g));
    memset(in,false,sizeof(in));
    for(int i = 0; i < m; i++)
     {
        int u,v,c;
        scanf("%d%d%d",&u,&v,&c);
        es[i] = edge(u,v,c);
        g[u][v] = g[v][u] = c; 
     }
   sort(es,es+m,cmp);
   int res = 0, cnt = 0;
   for(int i = 0; i < m; i++)
    {
       if(!same(es[i].u,es[i].v))
         {
            res += es[i].c;
            unit(es[i].u,es[i].v);
            in[es[i].u][es[i].v] = in[es[i].v][es[i].u] = true; 
            if(++cnt == n-1) break;
         }
    }
   for(int i = 1; i <= n; i++)
      {
         memset(vis,false,sizeof(vis));
         dfs(i,i,0);     
      } 
   //cout << res << endl;
   int ans = INF;
   for(int i = 0; i < m; i++)
         {
            if(in[es[i].u][es[i].v]) continue;
            ans = min(ans,res-md[es[i].u][es[i].v] + es[i].c);
            //cout << es[i].u << " " << es[i].v << " " << md[es[i].u][es[i].v] << endl; 
         } 
   if(ans != res) printf("%d\n",res);
   else printf("Not Unique!\n"); 
   }
   return 0;
}