PAT A1142 2020.04.02

这是一篇a1142的讲解,首先看一下题目:
“A clique(小集团) is a subset of vertices(顶点) of an undirected graph such that every two distinct(不同的) vertices in the clique are adjacent(相邻的). A maximal clique is a clique that cannot be extended by including one more adjacent vertex. Now it is your job to judge if a given subset of vertices can form a maximal clique.”
接下来是输入规范(Input Specification):
“Each input file contains one test case. For each case, the first line gives two positive integers Nv (≤ 200), the number of vertices in the graph, and Ne, the number of undirected edges. Then Ne lines follow, each gives a pair of vertices of an edge. The vertices are numbered from 1 to Nv.
After the graph, there is another positive integer M (≤ 100). Then M lines of query follow, each first gives a positive number K (≤ Nv), then followed by a sequence of K distinct vertices. All the numbers in a line are separated by a space.”
然后还有输出规范(Output Specification):
“For each of the M queries, print in a line Yes if the given subset of vertices can form a maximal clique; or if it is a clique but not a maximal clique, print Not Maximal; or if it is not a clique at all, print Not a Clique.”(在这里提到了3种输出格式)
最后是样例
Sample Input:

8 10
5 6
7 8
6 4
3 6
4 5
2 3
8 2
2 7
5 3
3 4
6
4 5 4 3 6
3 2 8 7
2 2 3
1 1 
3 4 3 6
3 3 2 1
1 1 在这个样例中,这组数据指的是一个孤立(没有任何边连向它)的点。

Sample Output:

Yes
Yes
Yes
Yes
Not Maximal
Not a Clique

思路分析:
因为这道题的数据量比较小,所以可以尝试一个比较暴力的方法。简单的说就是如果要对点进行一个比较,就比较每一个点;如果要找点,就从所有点中找。具体看代码分析。

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	int n, m;
	cin >> n >> m;//输入参数
	vector<vector<int>> gra(n + 1, vector<int>(n + 1, 0));//数组初始化
	for (int i = 1; i <= n; ++i) gra[i][i] = 1;//每个点本身置1
	while (m--) {
		int a, b;
		cin >> a >> b;
		gra[a][b] = 1;//建图
		gra[b][a] = 1;
	}
	int k;
	cin >> k;//输入参数
	while (k--) {
		int tmp;
		cin >> tmp;//输入参数,序列中元素个数
		vector<int> box(n + 5);//存储样例给出的点序列
		for (int i = 0; i < tmp; ++i) cin >> box[i];
		int sign = 0;//标志位
		for (int i = 0; i < tmp - 1; ++i) {
			for (int j = i + 1; j < tmp; ++j) {
				if (!gra[box[i]][box[j]]) {
					sign = 1;//如果点序列中有任意两点不连通,sign置1
					break;
				}
			}
		}
		if (sign) cout << "Not a Clique" << endl;
		else {
			int cnt = 0;//用来记录有多少个点与序列中的每个点都连通
			for (int i = 1; i <= n; ++i) {
				int choice = 0;//标志位,判断此点(i)是否与序列中每点都联通
				for (int j = 0; j < tmp; ++j) {
					if (!gra[i][box[j]]) {
						choice = 1;//如果不连通,choice置1
						break;
					}
				}
				if (!choice) ++cnt;//如果都连通,cnt加1
			}
			//如果给出的序列是最大的,cnt应该等于序列中元素的个数
			if (cnt > tmp) cout << "Not Maximal" << endl;
			else if (cnt == tmp) cout << "Yes" << endl;
		}
	}
	return 0;
}

注:第二个和第三个测试点查询序列中有单点的情况,但是这其中的某个单点不是孤立的点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值