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
这题虽然是考无向图,但是其实就是个模拟题,注意一下就行了;
思路:
首先先判断 not a clique
然后判断 not maximal
如果不是俩者那么就是yes
在判断not a clique 的时候其实只要遍历一下给的几个顶点是否都是相互连接的,如果不是那么就是not a clique
在判读not maximal 的时候,要用到散列表的思想hash 把给的顶点标记为1,没有给标记为0,然后在遍历的时候要用到逆向的思维,如果在没有给的顶点中找到与给的顶点都相连的,那么就是not maximal
最后输出yes
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
const int N = 250;
int mapp[N][N];
int v, e;
int h[N];
int main()
{
scanf("%d %d", &v, &e);
for (int i = 0;i < e;i++) {
int a, b;
scanf("%d %d", &a, &b);
mapp[a][b] = mapp[b][a] = 1;
}
int k;
scanf("%d", &k);
for (int i = 0;i < k;i++) {
int n;
scanf("%d", &n);
memset(h, 0, sizeof(h));
vector<int>p(n);
for (int j = 0;j < n;j++) {
scanf("%d", &p[j]);
h[p[j]] = 1;
}
int flag1 = 1;
for (int j = 0;j < n;j++) {
if (flag1 == 0) break;
for (int z = j + 1;z < n;z++) {
if (mapp[p[j]][p[z]] == 0) {
flag1 = 0;
printf("Not a Clique\n");
break;
}
}
}
if (flag1 == 0) continue;
int flag2 = 1;
for (int j = 1;j <= v;j++) {
if (h[j] == 0) {
for (int z = 0;z < n;z++) {
if (mapp[p[z]][j] == 0) break;
if (z == n - 1) flag2 = 0;
}
}
if (flag2 == 0) {
printf("Not Maximal\n");
break;
}
}
if (flag2 == 0) continue;
printf("Yes\n");
}
return 0;
}