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

Source: Internet
Author: User
Tags bool printf
B-the Unique MST Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64D &%i64u Submit Status Practice POJ 1679 appoint Description:system Crawler (2015-11-12)

Description Given A connected undirected graph, tell if it minimum spanning tree is unique. 

Definitio N 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of 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, UN Directed graph G = (V, E). The minimum spanning tree T = (V, E ') of G is the spanning tree, which has the and 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 <=), the number of test cases. Each case represents a graph. It begins with a line containing, integers n and m (1 <= n <=), the number of nodes and edges. Each of the following m lines contains a triple (xi, Yi, WI), indicating that Xi and Yi is connected by an edge with Weig HT = wi. For any of the nodes, there is at the most one edge connecting them.

Output for each input, if the MST are unique, print the total cost of it, or otherwise print the string ' not unique! '.

Sample Input

2
3 3
1 2 1
2 3 2 3
1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2

Sample Output

3 Not
unique!
This problem and csu1541 very much like, thought direct paste can be a, the result wa ...
Ideas:
With data
6 7
1 3 1
1 2 2
2 3 3
3 4 0
4 6 5
4 5 4
5 6 6
If we don't get a 0, will be 0 also selected into the minimum spanning tree, which will cause the original to choose 5 points on the end, but this is not considered 0 sides, the most also get 4 points can no longer take, so the answer and the previous minimum spanning tree value is the same, so you will think there is another smallest tree, is actually the same tree. So we're going to have to get rid of the 0 weights, because 0 of the side results are the same (when there are less than n edges).
AC Code:
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <
vector> typedef unsigned long long ll;
using namespace Std;
#define T 5500000 int n,m,c;
int par[t];
int nu[t];
	struct node {int u,v,w;
	BOOL operator< (const node& a) const{return w<a.w;
}}e[t];
	int find (int x) {int s = x;
	for (; s!=par[s];s=par[s]);
		while (s!=x) {int tmp = par[x];
		PAR[X] = s;
	x = tmp;
} return s;
	} int Kruskal (int x) {for (int i=0;i<=n;++i) {Par[i] = i;
	} int num = 0, cost = 0; for (int i=0;i<m;++i) {if (x==i| |!
		E[I].W) continue;//easy-wrong point int tx = FIND (e[i].u), Ty = Find (E[I].V);
			if (tx!=ty) {par[tx] = ty;
			Cost + = E[I].W;
			num++;
		if (x==-1) nu[c++] = i;
	} if (num==n-1) break;
} return cost;
 } int main () {#ifdef ZSC freopen ("Input.txt", "R", stdin), #endif int n,i;
  scanf ("%d", &n);
	  while (n--) {scanf ("%d%d", &n,&m);
	  c = 0; for (i=0;i<m;++i) {scanf ("%d%d%d", &e[i].u,&e[i]. V,&AMP;E[I].W);
	  } sort (e,e+m);
	  int w1 = Kruskal ( -1), W2;
	  BOOL flag = FALSE;
		  for (i=0;i<c;++i) {w2 = Kruskal (Nu[i]);
			  if (W2==W1) {printf ("not unique!\n"); flag =true;
		  Break
	  }} if (!flag) {printf ("%d\n", W1);
}} 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.