POJ 1679 the unique MST determines if the MST is unique

Source: Internet
Author: User

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 22817 Accepted: 8114

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!

Source

POJ monthly--2004.06.27 [email protected] Test Instructions: Give a graph, if there is only one MST diagram in the graph, enter the minimum cost, if not unique, enter not unique!O (n^3)find an MST graph, minimum cost ans, and record the edges of the MST graph, and then enumerate the edges of each MST graphif the edge is removed, the minimum cost of the graph is ans, which indicates that the MST graph is more than one, then breakFinally, if you enumerate each edge, the new minimum cost is always greater than ansindicates that there is only one MST diagram for the graph.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 5 using namespacestd;6 7 Const intmaxn=103;8 Const intinf=0x3f3f3f3f;9 Ten intCOST[MAXN][MAXN]; One intPRE[MAXN]; A intLOWC[MAXN]; - BOOLVIS[MAXN]; -  the voidInitintN) - { -      for(intI=1; i<=n;i++) -     { +          for(intj=1; j<=n;j++) -         { +             if(i==j) Acost[i][j]=0; at             Else -cost[i][j]=INF; -         } -     } - } -  in intPrimintNintCNT) - { tomemset (Vis,false,sizeof(Vis)); +     if(CNT) -memset (pre,-1,sizeof(pre)); thevis[1]=true; *     intans=0; $      for(intI=1; i<=n;i++)Panax Notoginseng     { -lowc[i]=cost[1][i]; the         if(CNT) +pre[i]=1; A     } the  +      for(intj=1; j<n;j++) -     { $         intp=-1; $         intMinc=INF; -          for(intI=1; i<=n;i++) -         { the             if(!vis[i]&&lowc[i]<minc) -             {WuyiMinc=Lowc[i]; thep=i; -             } Wu         } -         if(p==-1) About             return-1; $ans+=Minc; -vis[p]=true; -          for(intI=1; i<=n;i++) -         { A             if(!vis[i]&&cost[p][i]<Lowc[i]) +             { thelowc[i]=Cost[p][i]; -                 if(CNT) $pre[i]=p; the             } the         } the     } the     returnans; - } in  the intMain () the { About     inttest; thescanf"%d",&test); the      while(test--) the     { +         intn,m; -scanf"%d%d",&n,&M); the init (N);Bayi  the          for(intI=0; i<m;i++) the         { -             intu,v,w; -scanf"%d%d%d",&u,&v,&W); thecost[u][v]=cost[v][u]=W; the         } the  the         intAns=prim (N,1); -  the          for(intI=2; i<=n;i++) the         { the             intv=Pre[i];94             inttmp=Cost[i][v]; thecost[i][v]=cost[v][i]=INF; the             intSum=prim (N,0); the             if(ans==sum)98             { Aboutans=-1; -                  Break;101             }102cost[i][v]=cost[v][i]=tmp;103         }104         if(ans==-1) theprintf"Not unique!\n");106         Else107printf"%d\n", ans);108     }109     return 0; the}
View Code

POJ 1679 the unique MST determines if the MST is unique

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.