Poj 1679 the unique MST determines whether the minimum spanning tree is unique

Source: Internet
Author: User

If the length of each edge on the MST is equal to that of other edges, delete the edge and calculate the MST again. If it is the same as the original cost, is not unique

# Include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <climits> # include <string> # include <iostream> # include <map> # include <cstdlib> # include <list> # include <set> # include <queue> # include <stack> using namespace STD; typedef long ll; const int maxn = 105; const int maxk = 105*105; struct edge {int U, V, W; bool operator <(const edge & X) const {return W <X. W ;}}; edge e [Max K]; bool del [maxk], EQU [maxk], used [maxk]; int n, m, Fa [maxn]; int findp (int x) {return x = Fa [x]? X: Fa [x] = findp (Fa [x]);} int Kruskal (bool flag) {int ret = 0; For (INT I = 1; I <= N; I ++) Fa [I] = I; for (INT I = 0; I <m; I ++) if (! Del [I]) {int Pa = findp (E [I]. u), Pb = findp (E [I]. v); If (Pa = Pb) continue; If (FLAG) used [I] = true; Fa [PA] = Pb; RET + = E [I]. w;} return ret;} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); memset (Del, 0, sizeof (DEL); memset (equ, 0, sizeof (equ); memset (used, 0, sizeof (used); For (INT I = 0; I <m; I ++) {scanf ("% d", & E [I]. u, & E [I]. v, & E [I]. w);} Sort (E, E + M); For (INT I = 1; I <m; I ++) {If (E [I]. W = E [I-1]. w) {equ [I] = equ [I-1] = true ;}} int first = Kruskal (1); bool unique = true; For (INT I = 0; I <m; I ++) if (used [I] & equ [I]) {del [I] = true; int now = Kruskal (0 ); if (now = first) {unique = false; break ;}} if (unique) printf ("% d \ n", first); else puts ("not unique! ");} Return 0 ;}

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.