POJ1679 the Unique mst[sub-niche into a tree]

Source: Internet
Author: User

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 28673 Accepted: 10239

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] ask the niche into a tree to see if it is the same as the smallestMethod:Kruskal for the simultaneous mapping of MST, DFS to the root tree at the same time recursive f[i][j] for I to J in the path of the tree the maximum amount of weight O (n^2)The minor must be the smallest plus one edge minus one edge to get, enumeration plus which side compare W and F[u][v]
////main.cpp//poj1679////Created by Candy on 10/11/2016.//copyright©2016 Candy. All rights reserved.//#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intn= the, m=10005, inf=1e9;inlineintRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}intn,m,u,v,w;structedge{intV,w,ne;} E[n<<1];intH[n],cnt=0; inlinevoidInsintUintVintW) {CNT++; E[CNT].V=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=CNT; CNT++; E[CNT].V=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=CNT;}structdata{intu,v,w; BOOL operator< (ConstData &r)Const{returnw<R.W;}} A[M];intFa[n];inlineintFindintx) {returnfa[x]==x?x:fa[x]=find (Fa[x]);}intUse[n];intKruskal () {sort (a+1, A +1+m); intCnt=0, ans=0;  for(intI=1; i<=m;i++){        intu=a[i].u,v=a[i].v,w=A[I].W; intF1=find (u), f2=Find (v); if(F1==F2)Continue; FA[F1]=F2;        Ins (u,v,w); Use[i]=1; CNT++; ans+=W; if(cnt==n-1) Break; }    returnans;}intF[n][n],vis[n];voidDfsintu) {Vis[u]=1;  for(intI=h[u];i;i=e[i].ne) {        intv=e[i].v,w=E[I].W; if(Vis[v])Continue;  for(intx=1; x<=n;x++)if(Vis[x]) f[x][v]=f[v][x]=Max (F[X][U],W);    DFS (v); }}voidSol () {CNT=0; Memset (H,0,sizeof(h)); Memset (F,0,sizeof(f)); memset (Vis,0,sizeof(VIS)); memset (use,0,sizeof(use));  for(intI=1; i<=n;i++) fa[i]=i; intans=Kruskal (); DFS (1); intmn=INF;  for(intI=1; i<=m;i++)if(!Use[i]) {        intU=A[I].U,V=A[I].V,W=A[I].W;//printf ("Hi%d%d%d%d\n", U,v,w,f[u][v]);Mn=min (mn,w-F[u][v]); }    if(mn==0) puts ("Not unique!"); Elseprintf"%d\n", ans);}intMainintargcConst Char*argv[]) {    intt=read ();  while(t--) {n=read (); m=read ();  for(intI=1; i<=m;i++) {a[i].u=read (); A[i].v=read (); a[i].w=read ();}    Sol (); }    return 0;}

POJ1679 the Unique mst[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.