pat 1142 真的不用建图啊。。。

												1142 Maximal Clique (25 分)

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. (Quoted from https://en.wikipedia.org/wiki/Clique_(graph_theory))

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.

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
Sample Output:
Yes
Yes
Yes
Yes
Not Maximal
Not a Clique

这种题建图真的吃亏 表面上是图的题 但是实际上可以通过观察给的边和定点推出一个关系
比如像这题 看了下几个题解和我的不太一样 你可以试试理解。 个人觉得更为简便 所以就贴出来了
用a和b vector来存各条边的点 a[0]<->b[0] 这样
然后用一个same数组 和 maybe数组
遍历各条边
:两个这条边上的两个端点都属于查找集的点 same以两个点为下标的值+1
:其中只有一个点属于查找集,另一个点对应的maybe数组值+1

如果不是最大的话,那么maybe值里面会有一个点的下表会等于num(查找集点的个数)

如果是clique的话各个点对应在same里面的值都是num 如果有一个不是num就不是clique 比如4 5 4 3 6这组数据 遍历完各个点对应的same值为3 不难想象 因为一个点和另外三个点有三条边嘛 也就是4-1 这时候再判断如果不是最大 输出not max

我语文很垃圾
还是最好自行代码理解吧。。

#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
using namespace std;
const int maxn = 201;
int same[maxn],maybe[maxn];


vector<int> a,b;
int Nv,Ne;
set<int> s;

int main()
{
	scanf("%d%d",&Nv,&Ne);
	int id1,id2;
	for(int i=0;i<Ne;i++)
	{
		scanf("%d%d",&id1,&id2);
		a.push_back(id1);
		b.push_back(id2);
	}
	int k;
	scanf("%d",&k);
	int num,temp;
	for(int j=0;j<k;j++)
	{
		s.clear();
		fill(same,same+maxn,0);
		fill(maybe,maybe+maxn,0);
		scanf("%d",&num);
		for(int i=0;i<num;i++)
		{
			scanf("%d",&temp);
			s.insert(temp);
		}
		for(int i=0;i<a.size();i++)
		{
			if(s.count(a[i])&&s.count(b[i]))
			{
				same[a[i]]++;
				same[b[i]]++;
			}
			else if(s.count(a[i]))
			{
				maybe[b[i]]++;
			}
			else if(s.count(b[i]))
			{
				maybe[a[i]]++;
			}
		}
		bool flag1 = false;
		for(int i=1;i<=Nv;i++)
		{
			if(maybe[i]>=num)
			{
				flag1 = true;
				break;
			}
		}
//		if(flag)
//		{
//			printf("Not Maximal\n");
//			continue;
//		}
		bool flag=false;
		for(auto it:s)
		{
			if(same[it]!=num-1)
			{
				flag = true;
				break;
			}
		}
		if(!flag)
		{
			if(flag1)
				printf("Not Maximal\n");
			else
				printf("Yes\n");
		}
		else
		{
			printf("Not a Clique\n");
		}
		
		
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章