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!
題目意思:
給一個圖,判斷其是否只有一個最小生成樹還是有多顆最小生成樹。如果只有一顆,輸出最小生成樹的權(quán)值,否則輸出“Not Unique!”。
代碼如下:
#include<stdio.h>
#define max 10001
typedef 
struct M
{
    
int x, y, w;
}
M;
M N[max], NN[max];
int Weight, sum, num, T, pre, L[max], Father[101];
int cmp(const void * a, const void * b)
{
    
return ((M*)a)->- ((M*)b)->w;
}

void MakeSet(int x)
{
    Father[x] 
= -1;
}

int FindSet(int x)
{
    
if (Father[x] < 0)
    
{
        
return x;
    }

    
return Father[x] = FindSet(Father[x]);
}

void Union(int x, int y, int w)
{
    
int a = FindSet(x), b = FindSet(y);
    
if (a!= b)
    
{
        sum
++;
        Weight 
+= w;
        T 
= 1;
        
if (Father[a] <= Father[b])
        
{
            Father[a] 
+= Father[b];
            Father[b] 
= a;
        }

        
else
        
{
            Father[b] 
+= Father[a];
            Father[a] 
= b;
        }

    }

}

int Run(int n, int m)
{
    
int i, j, k;
    
for (i = 0; i < num; i++)
    
{
        k 
= 0;
        
for (j = 0; j < m; j++)
        
{
            
if (j != L[i])
            
{
                NN[k
++= N[j];
            }

        }

        
for (j = 1; j <= n; j++)
        
{
            MakeSet(j);
        }

        sum 
= 0;
        Weight 
= 0;
        
for (j = 0; j < k; j++)
        
{
            Union(NN[j].x, NN[j].y, NN[j].w);
            
if (sum == n - 1 && Weight == pre)
            
{
                
return 1;
            }

        }

    }

    
return 0;
}
        
int main()
{
    
int t, n, m, i;
    scanf(
"%d"&t);
    
while (t--)
    
{
        scanf(
"%d%d"&n, &m);
        
for (i = 0; i < m; i++)
        
{                    
            scanf(
"%d%d%d"&N[i].x, &N[i].y, &N[i].w);
        }

        
for (i = 1; i <= n; i++)
        
{
            MakeSet(i);
        }

        qsort(N, m, 
sizeof(M), cmp);
        num 
= 0;
        sum 
= 0;
        Weight 
= 0;
        pre 
= 0;
        
for (i = 0; i < m; i++)
        
{
            T 
= 0;
            Union(N[i].x, N[i].y, N[i].w);
            
if (T)
            
{
                L[num
++= i;
            }

            
if (sum == n - 1)
            
{
                pre 
= Weight;
                
break;
            }

        }

        printf(Run(n, m) 
? "Not Unique!\n" : "%d\n", pre);
    }

    
return 0;
}