PAT TOP 1016. Uniqueness of MST (35)

问题描述:

1016. Uniqueness of MST (35)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

Given any weighted undirected graph, there exists at least one minimum spanning tree (MST) if the graph is connected. Sometimes the MST may not be unique though. Here you are supposed to calculate the minimum total weight of the MST, and also tell if it is unique or not.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N (<= 500), and M, which are the total number of vertices, and the number of edges, respectively. Then M lines follow, each describes an edge by 3 integers:

V1 V2 Weight

where V1 and V2 are the two ends of the edge (the vertices are numbered from 1 to N), and Weight is the positive weight on that edge. It is guaranteed that the total weight of the graph will not exceed 230.

Output Specification:

For each test case, first print in a line the total weight of the minimum spanning tree if there exists one, or else print "No MST" instead. Then if the MST exists, print in the next line "Yes" if the tree is unique, or "No" otherwise. If there is no MST, print the number of connected components instead.

Sample Input 1:
5 7
1 2 6
5 1 1
2 3 4
3 4 3
4 1 7
2 4 2
4 5 5
Sample Output 1:
11
Yes
Sample Input 2:
4 5
1 2 1
2 3 1
3 4 2
4 1 2
3 1 3
Sample Output 2:
4
No
Sample Input 3:
5 5
1 2 1
2 3 1
3 4 2
4 1 2
3 1 3
Sample Output 3:
No MST
2

这一题不仅要用 生成树算法 求出最小生成树,还要判断 最小生成树 是否唯一,更要在最小生成树不存在时,求出图的强连通分量。。。所以还是比较麻烦的一题。

整体思路如下:

(1)先以1号结点为初始点,用prim算法,求出最小生成树;

(2)如果,图中仍有结点未被访问,则最小生成树不存在;转而循环地对每个未被访问结点,执行prim算法,每次循环结束,part自增;当图中没有未被访问结点时,part就是图的强连通分量;

(3)如果,图中没有结点未被访问,则最小生成树存在;转而求其唯一性,对每条"不在prim最小生成树中"而且"边长等于prim最小生成树中的边的边长"的边,将它加入prim最小生成树中,prim最小生成树便会含有环路,在那条环路中用bfs算法寻找边长等于新加入的边的边长的一条边,如果存在,则说明将这两条边互换,最小生成树的性质仍然保持,既是最小生成树不唯一;反之,最小生成树唯一;

AC代码:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<bits/stdc++.h>
using namespace std;
vector<vector<pair<int,int> > > v;
vector<vector<pair<int,int> > > vst;
vector<bool> vb;
unordered_set<int> us;
int mst;
struct node
{
	int id;
	int pid;
	int w;
	bool operator< (const node& en) const
	{
		return w>en.w;
	}
} no;
vector<node> vsst;
inline int st(int n)
{
	priority_queue<node> q;
	no.id=n;
	no.pid=n;
	no.w=0;
	q.push(no);
	for(;!q.empty();)
	{
		auto qf=q.top();
		q.pop();
		if(vb[qf.id])
		{
			mst=mst+qf.w;
			us.emplace(qf.w);
			vb[qf.id]=false;
			if(qf.id!=qf.pid)
			{
				vst[qf.id].emplace_back(make_pair(qf.pid,qf.w));
				vst[qf.pid].emplace_back(make_pair(qf.id,qf.w));
			}
			for(auto k:v[qf.id])
			{
				if(vb[k.first])
				{
					no.id=k.first;
					no.pid=qf.id;
					no.w=k.second;
					q.push(no);
				}
			}
		}
		else
		{
			if(us.find(qf.w)!=us.end())
			vsst.emplace_back(qf);
		}
	}
	for(int i=n;i<vb.size();++i)
	if(vb[i])
	return i;
	return -1;
}
inline bool bfs(int c1,int c2,int w)
{
	vector<int> vbb(v.size(),0);
	queue<pair<int,int> > q;
	q.push(make_pair(c1,1));
	q.push(make_pair(c2,-1));
	vbb[c1]=1;
	vbb[c2]=-1;
	for(;!q.empty();)
	{
		auto qf=q.front();
		q.pop();
		for(auto k:vst[qf.first])
		{
			if(vbb[k.first]*qf.second==0)
			{
				if(k.second==w)
				qf.second=2*qf.second;
				vbb[k.first]=qf.second;
				q.push(qf);
			}
			else if(vbb[k.first]*qf.second<0)
			{
				if(abs(vbb[k.first])>1)
				return false;
				else
				return true;
			}
		}
	}
	return false;
}
int main()
{
//	freopen("data.txt","r",stdin);
	int n,m,c1,c2,w;
	mst=0;
	scanf("%d %d",&n,&m);
	v.resize(n);
	vb.resize(n,true);
	vst.resize(n);
	for(;m--;)
	{
		scanf("%d %d %d",&c1,&c2,&w);
		c1--;
		c2--;
		v[c1].emplace_back(make_pair(c2,w));
		v[c2].emplace_back(make_pair(c1,w));
	}
	w=st(0);
	if(w<0)
	{
		printf("%d\n",mst);
		bool flag=true;
		for(int i=0;i<vsst.size()&&flag;++i)
		{
			if(bfs(vsst[i].id,vsst[i].pid,vsst[i].w))
			continue;
			else
			flag=false;
		}
		if(flag)
		printf("Yes");
		else
		printf("No");
	}
	else
	{
		printf("No MST\n");
		int part=1;
		for(;w>-1;part++)
		w=st(w);
		printf("%d",part);
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值