Poj1679 the unique MST [secondary generation tree]

Source: Internet
Author: User

The unique MST
Time limit:1000 ms   Memory limit:10000 K
Total submissions:20421   Accepted:7183

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 [email protected]

This is a good question. Once ac ~

A connected graph is given to determine whether the minimum spanning tree is unique.

Solution: Find the minimum spanning tree and then calculate the secondary spanning tree. If the length of the secondary spanning tree is equal to that of the Minimum Spanning Tree, it indicates that it is not unique; otherwise, it is unique.

This is my first step towards generating a tree. Here we will summarize this algorithm:

Use a matrix MAX [] [] to indicate the longest edge weight (key!) on any two-point path in the Minimum Spanning Tree !!), When the Minimum Spanning Tree is obtained, the selected edge is marked as used. After the result is obtained, the unused remaining edge is traversed. If this edge is added to the minimum tree, a loop must be formed, therefore, you need to remove the maximum value in the loop in the original tree, that is, the value stored in the max matrix. Therefore, the problem is converted to finding an unused edge, this minimizes the difference between the edge value and the edge value corresponding to the max matrix. After traversal, the value of the secondary spanning tree is the minimum difference added to the value of the original spanning tree.

#include <stdio.h>#include <string.h>#include <limits.h>#define maxn 102#define maxm (maxn * maxn) >> 1int head[maxn], max[maxn][maxn];struct Node{int u, v, cost, next;bool vis;} E[maxm];bool vis[maxn];int mini(int a, int b){return a < b ? a : b;}int prim(int n, int m){int u, i, tmp, j, len = 0, count = 0;memset(max, 0x7f, sizeof(max));memset(vis, 0, sizeof(vis));vis[1] = 1;while(count < n - 1){for(i = 1, tmp = INT_MAX; i <= n; ++i){if(!vis[i]) continue;for(j = head[i]; j != -1; j = E[j].next){if(vis[E[j].v]) continue;if(E[j].cost < tmp){tmp = E[j].cost; u = j;}}}++count; len += tmp;for(i = 1; i <= n; ++i){if(!vis[i]) continue;max[i][E[u].v] = max[E[u].v][i] = E[u].cost;}vis[E[u].v] = 1; E[u].vis = 1;}return len;}int getSecLen(int n, int m){int min = INT_MAX, u, v, w;for(int i = 0; i < m; ++i){if(E[i].vis) continue;u = E[i].u; v = E[i].v;w = E[i].cost;min = mini(min, w - max[u][v]);if(min == 0) return 0;}return min;}int main(){int t, n, m, i, minLen, secLen;scanf("%d", &t);while(t--){scanf("%d%d", &n, &m);memset(head, -1, sizeof(head));for(i = 0; i < m; ++i){scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].cost);E[i].vis = 0; E[i].next = head[E[i].u];head[E[i].u] = i;}minLen = prim(n, m);secLen = getSecLen(n, m);if(secLen == 0) printf("Not Unique!\n");else printf("%d\n", minLen);}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.