< topic links >
Main topic:
Given an no-map, determine whether its smallest spanning tree is unique.
Problem Solving Analysis:
For each edge in the diagram, scan the other edges, mark the edge if there is an edge with the same weight, and use Kruskal to find the MST.
If there is no marked edge in the MST, the MST is unique; otherwise, the edge of the tag is removed in the MST, then the MST, and if the MST weight is the same as the original MST weight, the MST is not unique.
#include <cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespacestd;Const intn=11000;intn,m,cnt;intParent[n];BOOLFlag;structEDGE {intu,v,w; intEq,used,del;} Edge[n];BOOLCMP (EDGE A,edge b) {returna.w<B.W;}intFind (intx) {if(parent[x]! = x) Parent[x] =Find (parent[x]); returnparent[x];}voidUnion (intXinty) {x=Find (x); Y=Find (y); if(x = = y)return; Parent[y]=x;}intKruskal () { for(intI=0; i<=10005; i++) parent[i]=i; intsum=0, num=0; for(intI=0; i<m;i++){ if(edge[i].del==1)Continue; intu=edge[i].u;intV=EDGE[I].V;intw=EDGE[I].W; if(Find (u)! =Find (v)) {Sum+=W; if(!flag) edge[i].used=1; Num++; Union (U,V); } if(num>=n-1) Break; } returnsum;}intMain () {intt,d; CIN>>T; while(t--) {CNT=0; CIN>>n>>m; for(intI=0; i<m; i++) {cin>>edge[i].u>>edge[i].v>>EDGE[I].W; Edge[i].del=0; Edge[i].used=0; Edge[i].eq=0;//determine if there is a side that is the same length as it } for(intI=0; i<m;i++){ for(intj=0; j<m;j++){ if(I==J)Continue; if(EDGE[I].W==EDGE[J].W) edge[i].eq=1; }} sort (Edge,edge+m,cmp); Flag=false; CNT=Kruskal (); Flag=true; BOOLfp=false; for(intI=0; i<m;i++){ if(edge[i].used==1&&edge[i].eq==1) {Edge[i].del=1; ints=Kruskal (); if(s==CNT) {FP=true; printf ("Not unique!\n"); Break; } Edge[i].del=0; } } if(!FP) cout<<cnt<<Endl; } return 0;}
2018-10-01
POJ-1679 the unique MST (judging the uniqueness of the minimum spanning tree)