The Unique MST POJ - 1679 (次小生成樹模板)

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 32148   Accepted: 11633

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

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!

Source

題目大意:現在給你n個點,m個邊,然後判斷是否最小生成樹是唯一的

解題思路:判斷是否最小生成樹是唯一的,需要判斷是次小生成樹是否與最小生成樹相同即可,首先我們需要生成一個最小生成樹,這樣的話,我們對於最小生成樹中的每個邊進行枚舉刪除,然後在刪除掉最小生成樹的一個邊的情況下生成的最小生成樹是否唯一即可

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;


struct point
{
	int x,y,c;
}a[10005];
int sum,n,m,cnt;
int check[10005],path[10005];;
void init()
{
	for(int i=1;i<=n;i++)
		 check[i]=i;
}
int find(int x)
{
	if(check[x]==x)
		return x;
	check[x]=find(check[x]);
	return check[x];
}
bool cmp(point x,point y)
{
	return x.c<y.c;
}
bool merage(int x,int y)
{
	int t1=find(x);
	int t2=find(y);
	if(t1!=t2)
	{
		check[t2]=t1;
		return true;
	}
	else
		return false;
}
void Kruskal()
{
	int i;
	init();
	sort(a+1,a+1+m,cmp);
	for(i=1;i<=m;i++)
	{
		if(merage(a[i].x,a[i].y))
		{
			cnt++;
			path[cnt]=i;//將生成最小生成樹的邊的下標記下來
			sum+=a[i].c;//算出最小生成樹的權值和
		}
		if(cnt==n-1)
			break;
	}
}
bool mst()
{
	int ans,i,j;
	int k;
	for(i=1;i<=cnt;i++)//枚舉最小生成樹中的邊
	{
		ans=0,k=0;
		init();
		for(j=1;j<=m;j++)
		{
			if(j==path[i]) continue;
			if(merage(a[j].x,a[j].y))
			{
				k++;
				ans+=a[j].c;
			}
		}
		if(k==n-1&&ans==sum)//判斷在刪除一個邊的情況下,是否生成了一個次小生成樹與最小生成樹的權值相同
			return false;
	}
	return true;
}
int main()
{
	int T,i;
	cin>>T;
	while(T--)
	{
		memset(check,0,sizeof(check));
		memset(a,0,sizeof(a));
		memset(path,0,sizeof(path));
		sum=0;
		cnt=0;
		cin>>n>>m;
		for(i=1;i<=m;i++)
		{
			cin>>a[i].x>>a[i].y>>a[i].c;
		}
		Kruskal();//預處理出來一個最小生成樹
		if(mst())
		{
			cout<<sum<<endl;
		}
		else
		{
			cout<<"Not Unique!"<<endl;
		}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章