POJ-1679 the unique MST (judging the uniqueness of the minimum spanning tree)

Source: Internet
Author: User

< 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)

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.