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");
		}
		
		
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章