POJ_1679_The Unique MST (Second Small Spanning Tree)

Time Limit: 1000MS   Memory Limit: 10000K
         

 

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!
     
     
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define maxn 111
#define inf 0x3f3f3f3f

int map[maxn][maxn],mmax[maxn][maxn];//map adjacency matrix to store the graph, mmax represents the maximum edge weight from i to j in the minimum spanning tree
bool used[maxn][maxn];//Determine whether the edge is added to the minimum spanning tree
int pre[maxn], dis[maxn];//pre is used for the construction of mmax, and the previous node placed in MST is installed, and dis is used to build MST

void init(int n)
{
	for (int i=1;i<=n;i++)//Graph initialization
	{
		for (int j=1;j<=n;j++)
		{
			if (i==j)
			{
				map[i][j]=0;
			}
			else
			{
				map[i][j]=inf;
			}
		}
	}
}

void read(int m)
{
	int u,v,w;
	for (int i=0;i<m;i++)//读入图
	{
		scanf("%d%d%d",&u,&v,&w);
		map[u][v]=map[v][u]=w;
	}
}
int prime(int n)//build MST
{
	int ans=0;
	bool vis[maxn];
	memset(vis,false,sizeof(vis));
	memset(used,false,sizeof(used));
	memset(mmax,0,sizeof(mmax));
	for (int i=2;i<=n;i++)
	{
		dis[i]=map[1][i];
		pre[i]=1;//1 point is the first point put into MST, first set as the precursor node of all points
	}
	pre[1]=0;
	dis[1]=0;
	vis [1] = true;
	for (int i=2;i<=n;i++)
	{
		int min_dis = inf, k;
		for (int j=1;j<=n;j++)
		{
			if (vis[j]==0&&min_dis>dis[j])
			{
				min_dis = dis [j];
				k=j;
			}
		}
		if (min_dis==inf)//If there is no minimum spanning tree
		{
			return -1;
		}
		ans+=min_dis;
		vis[k]=true;
		used[k][pre[k]]=used[pre[k]][k]=true;//Points marked as put into MST
		for (int j=1;j<=n;j++)
		{
			if (vis[j])
			{
				mmax[j][k]=mmax[k][j]=max(mmax[j][pre[k]],dis[k]);//Maximum edge of minimum spanning tree ring
			}
			if (!vis[j]&&dis[j]>map[k][j])
				{
					dis[j]=map[k][j];
					for [j] = k;
				}
		}
	}
	return ans;//The sum of the weights of the minimum spanning tree
}
int smst(int n,int min_ans)//min_ans is the weight and the minimum spanning tree
{
	int ans=inf;
	for (int i=1;i<=n;i++)//Enumerate the edges outside the minimum spanning tree
	{
		for (int j=i+1;j<=n;j++)
		{
			if (map[i][j]!=inf&&!used[i][j])
			{
				ans=min(ans,min_ans+map[i][j]-mmax[i][j]);//The weight of the next-smallest MST of the edge is MST plus the edge and minus the maximum value of the ring where the edge is located MST side
			}
		}
	}
	if (ans==inf)
	{
		return -1;
	}
	return ans;
}
void solve(int n)
{
	int ans=prime(n);
	if (ans==-1)
	{
		puts("Not Unique!");
		return;
	}
	if (smst(n,ans)==ans)//The weight of the second smallest MST is equal to the MST, indicating that the MST is not unique
	{
		printf("Not Unique!\n");
	}
	else
	{
		printf("%d\n",ans);
	}
}
intmain()
{
	int t,n,m;
	
	scanf("%d",&t);
	while (t--)
	{
		scanf("%d%d",&n,&m);
		init(n);
		read(m);
		solve(n);
	}
	
	return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326434613&siteId=291194637