[POJ1679] The Unique MST sub-niche into a tree

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=1679

Give you a graph of connectivity, asking you whether the minimum spanning tree of this graph is unique.

If the minimum spanning tree is unique, that is, the spanning tree communicates the weights and uniqueness of all nodes. If not unique, then there is another minimum spanning tree so that the weights are equal to the weights of the previous minimum spanning tree.

Another thought, that is, the weight of the sub-niche tree is the same as the minimum spanning tree weights, then the problem becomes the second niche into a tree weight.

I chose to first find the smallest spanning tree and save the edges that are used on the tree. Then take each of the edges you use, and then find the minimum spanning tree. The minimum spanning tree is not unique if the selected edge weights of the spanning tree that do not contain the currently deleted edges are the same as the selected edge weights of the smallest spanning tree.

Code:

1#include <algorithm>2#include <iostream>3#include <iomanip>4#include <cstring>5#include <climits>6#include <complex>7#include <fstream>8#include <cassert>9#include <cstdio>Ten#include <bitset> One#include <vector> A#include <deque> -#include <queue> -#include <stack> the#include <ctime> -#include <Set> -#include <map> -#include <cmath> +  - using namespacestd; +  AtypedefstructNode { at     intu; -     intv; -     intW; - }node; -  - BOOLCMP (node n1, node N2) { in     returnN1.W <N2.W; - } to  + Const intMAXN =111; -Node node[6666]; the intVIS[MAXN]; * intN, M, U, V, W; $ intPRE[MAXN];Panax Notoginseng intcnt, dig, flag, ans; -  the voidinit () { +      for(inti =0; I <= MAXN; i++) { APre[i] =i; the     } + } -  $ intFindintx) { $     returnx = = Pre[x]? X:PRE[X] =find (Pre[x]); - } -  the voidUniteintXinty) { -x =find (x);Wuyiy =find (y); the     if(x! = y) Pre[y] =x; - } Wu  - intMain () { About     //freopen ("in", "R", stdin); $     intT; -scanf"%d", &T); -      while(t--) { - init (); Amemset (Vis,0,sizeof(Vis)); +memset (node,0,sizeof(node)); theDig =0; -CNT =0; $Flag =0; thescanf"%d%d", &n, &m); the          for(inti =0; I < m; i++) { thescanf" %d%d%d", &u, &v, &W); theNODE[I].U =u; -NODE[I].V =v; inNODE[I].W =W; the         } theSort (node, node+m, CMP); About          for(inti =0; I < m && CNT < n; i++) { the             if(Find (node[i].u)! =find (NODE[I].V)) { the Unite (NODE[I].U, NODE[I].V); thevis[cnt++] =i; +Dig + =NODE[I].W; -             } the         }BayiAns =dig; the  the          for(inti =1; I < n; i++) { - init (); -CNT = n-1; theDig =0; the              for(intj =0; J < m && CNT; J + +) { the                 if(Vis[i]! = J && Find (node[j].u)! =find (NODE[J].V)) { the Unite (NODE[J].U, NODE[J].V); -Dig + =NODE[J].W; thecnt--; the                 } the             }94             if(CNT = =0&& Dig = =ans) { theFlag =1; the                  Break; the             }98         } About  -         if(flag) printf ("Not unique!\n");101         Elseprintf"%d\n", ans);102     }103}

[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.