POJ1679 (sub-niche into a tree)

Source: Internet
Author: User

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 24201 Accepted: 8596

Description

Given a connected undirected graph, tell if it minimum spanning tree is unique.

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of 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, which has the and 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 <=), the number of test cases. Each case represents a graph. It begins with a line containing, integers n and m (1 <= n <=), the number of nodes and edges. Each of the following m lines contains a triple (xi, Yi, WI), indicating that Xi and Yi is connected by an edge with Weig HT = wi. For any of the nodes, there is at the most one edge connecting them.

Output

For each input, if the MST was 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!
Test instructions: Determines whether there is a unique minimum spanning tree.
Idea: First to find a minimum spanning tree. In this tree first join (x, y), join will be a ring, if delete (x, y) of the largest edge, you will be added (x, y) when the weight of the lowest value of a tree. If the joined edges are equal to the deleted edges, the minimum build is not unique.
Harvest: Learn the sub-niche into a tree.
#include <cstdio>#include<iostream>#include<cstdlib>#include<algorithm>#include<ctime>#include<cmath>#include<string>#include<cstring>#include<stack>#include<queue>#include<list>#include<vector>#include<map>#include<Set>using namespacestd;Const intinf=0x3f3f3f3f;Const Doubleeps=1e-Ten;Const DoublePi=acos (-1.0);#defineMAXN 500intN, M;intA[MAXN][MAXN];intUSED[MAXN][MAXN];intMMAX[MAXN][MAXN];intVIS[MAXN];intDIS[MAXN];intPRE[MAXN];intPrim () {intAns =0; memset (Vis,0,sizeofvis); memset (Used,0,sizeofused); memset (Mmax,0,sizeofMmax);  for(inti =2; I <= N; i++) {Dis[i]= a[1][i]; Pre[i]=1; } vis[1] =1; dis[1] =0;  for(inti =1; I < n; i++)    {        inttemp =INF; intK =-1;  for(intj =1; J <= N; J + +)        {            if(!vis[j] && temp >Dis[j]) {Temp=Dis[j]; K=J; }        }        if(k = =-1)returnans; Ans+=temp; VIS[K]=1; USED[K][PRE[K]]= Used[pre[k]][k] =1; MMAX[PRE[K]][K]=temp;  for(intj =1; J <= N; J + +) Mmax[j][k]=Max (mmax[j][pre[k]],mmax[pre[k]][k]);        //Find the edge of the largest Benquan.  for(intj =1; J <= N; J + +)        {            if(!vis[j] && dis[j] >A[k][j]) {Dis[j]=A[k][j]; PRE[J]=K; }        }    }    returnans;}intMain () {intT; scanf ("%d", &t);  while(t--) {scanf ("%d%d", &n, &m); intu, V, W; Memset (A, INF,sizeofa);  for(inti =0; I < m; i++) {scanf ("%d%d%d", &u, &v, &W); A[U][V]= A[v][u] =W; }        intMST =Prim (); intFlag =0;  for(inti =1; I <= N; i++)             for(intj = i+1; J <= N; J + +)            {                if(A[i][j] = = INF | | used[i][j] = =1)                    Continue; if(A[i][j] = =Mmax[i][j]) //Determines whether the joined edges and deleted edges are equal. {flag=1;  Break; }            }        if(flag) printf ("Not unique!\n"); Elseprintf ("%d\n", MST); }    return 0;}

POJ1679 (sub-niche into a tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.