Poj1679 The Unique MST (decision generation tree), poj1679mst

Source: Internet
Author: User

Poj1679 The Unique MST (decision generation tree), poj1679mst

The Unique MST
Time Limit:1000 MS   Memory Limit:10000 K
Total Submissions:23180   Accepted:8235

Description

Given a connected undirected graph, tell if its minimum spanning tree is unique.

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E ). A spanning tree of G is a 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 that has 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 <= 20), the number of test cases. each case represents a graph. it begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. for any two nodes, there is at most one edge connecting them.

Output

For each input, if the MST is 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 srbga @ POJ

For the Minimum Spanning Tree (we can use the kruskal and prime algorithms. Here we use kruskal. If you don't want to use Baidu .), The sum and minimum of edge values are called the Minimum Spanning Tree.

The secondary spanning tree is the minimum spanning tree except the Minimum Spanning Tree. In addition, all secondary spanning trees are obtained by the edge of the Minimum Spanning Tree.

So the difficulty is how to change edges.

How to change edges:

1. first obtain the minimum spanning tree with the value of x.

2. Add edges that are not on the generated tree by enumeration (a ring is formed at this time)

3. Compare the maximum weight of the (edge in the least generated tree) value on the search ring with the weight of the edge added to the non-generated tree. The difference is min.

Because we use one-to-one enumeration to add edges and min has multiple edges, we can find the smallest one, so the next generation tree is x + min.


Although I took this question A yesterday, I saw the test data in the discussion board and found that I had never done it again, but AC. Then I got up today to study...

I found that my program is looking for the maximum value, but if a ring has a branch, it will also find the maximum value in the branch, so I optimized it again.

Priority queue.

First, attach the first code:

# Include <stdio. h> # include <string. h ># include <algorithm> using namespace std; struct node {int a, B, cost;} c [10005]; int fa [105], tree [105] [105], vis [10005], vis_tree [105]; // The vis array is the marker of m for data. vis_tree is the marker of int n for the Minimum Spanning tree, m, max1; bool cmp (node x, node y) {if (x. cost <y. cost) return true; elsereturn false;} int find (int x) // find the root {if (fa [x]! = X) fa [x] = find (fa [x]); return fa [x];} void sec_tree (int a, int B) // query the maximum value of an edge in the Spanning Tree (I am doing something wrong here. If a ring has a branch, it will also be searched) {vis_tree [a] = 1; if (a = B) return; for (int I = 1; I <= n; I ++) if (tree [a] [I] &! Vis_tree [I]) {if (max1 <tree [a] [I]) max1 = tree [a] [I]; sec_tree (I, B );}} int main () {int ncase; scanf ("% d", & ncase); while (ncase --) {memset (vis, 0, sizeof (vis )); memset (tree, 0, sizeof (tree); memset (& c, 0, sizeof (& c); scanf ("% d", & n, & m); for (int I = 1; I <= n; I ++) fa [I] = I; for (int I = 0; I <m; I ++) scanf ("% d", & c [I]. a, & c [I]. b, & c [I]. cost); sort (c, c + m, cmp); int sum = 0; for (int I = 0; I <m; I ++) // obtain the minimum spanning tree using the kruskal algorithm {int x = find (c [I]. a); int y = Find (c [I]. B); if (x! = Y) {fa [x] = y, sum + = c [I]. cost; tree [x] [y] = tree [y] [x] = c [I]. cost; vis [I] = 1 ;}} int flag = 0; for (int I = 0; I <m; I ++) {if (! Vis [I]) // compare the maximum values of the edges and formed rings in the Spanning Tree. If the values are equal, MST is not unique {max1 =-1; memset (vis_tree, 0, sizeof (vis_tree); sec_tree (c [I]. a, c [I]. b); if (max1 = c [I]. cost) {flag = 1; break ;}} if (! Flag) printf ("% d \ n", sum); elseprintf ("Not Unique! \ N ");}}

This is the optimized code. The comments are the same as above, and they are different from each other:

# Include <stdio. h> # include <string. h >#include <algorithm> # include <queue> using namespace std; struct node1 {int a, B, cost; friend bool operator <(node1 x, node1 y) {return x. cost <y. cost ;}}; priority_queue <node1> s; struct node {int a, B, cost;} c [10005]; int fa [105], tree [105] [105], vis [10005], vis_tree [105]; int n, m, max1, flag1; bool cmp1 (node x, node y) {if (x. cost <y. cost) return true; elsereturn false;} int find (int x) {If (fa [x]! = X) fa [x] = find (fa [x]); return fa [x];} void sec_tree (int a, int B) {node1 temp; vis_tree [a] = 1; if (a = B) // if a = B is found, Mark {flag1 = 1;} for (int I = 1; I <= n; I ++) if (tree [a] [I] &! Vis_tree [I]) {temp. a = a, temp. B = I, temp. cost = tree [a] [I]; s. push (temp); if (! Flag1) // It is different here from above. If a = B cannot be found, the previous s will be restored. pop (), vis_tree [I] = 0, sec_tree (I, B) ;}} int main () {int ncase; scanf ("% d", & ncase ); while (ncase --) {memset (vis, 0, sizeof (vis); memset (tree, 0, sizeof (tree); memset (& c, 0, sizeof (& c); scanf ("% d", & n, & m); for (int I = 1; I <= n; I ++) fa [I] = I; for (int I = 0; I <m; I ++) scanf ("% d", & c [I]. a, & c [I]. b, & c [I]. cost); sort (c, c + m, cmp1); int sum = 0; for (int I = 0; I <m; I ++) {int x = find (c [I]. a); int y = find (C [I]. B); if (x! = Y) {fa [x] = y, sum + = c [I]. cost; tree [x] [y] = tree [y] [x] = c [I]. cost; vis [I] = 1 ;}} int flag = 0; for (int I = 0; I <m; I ++) {if (! Vis [I]) {int flag1 = 0; while (! S. empty () s. pop (); memset (vis_tree, 0, sizeof (vis_tree); sec_tree (c [I]. a, c [I]. b); node1 temp; temp = s. top (); if (temp. cost = c [I]. cost) {flag = 1; break ;}} if (! Flag) printf ("% d \ n", sum); elseprintf ("Not Unique! \ N ");}}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.