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
Maximal Clique:极大团,结点两两相连的最大集合。一个图中可以有多个极大团。
Maximum clique:最大团,size最大的极大团。
题目很明确给定集合判断是否是一个Maximal Clique。
1、用一个二维数组g[][]存储图,g[i][0]保存结点i连接的结点个数,即其边数;
2、用一个一维数组a[]存储询问集合;用一个临时变量p记录集合中满足与除集合外的其他点有连接的点的个数
- 先判断是否是一个团
- 是一个团
- p==k,满足极大团输出"Yes",否则输出"Not Maximal"(存在一个点不在集合中且与集合中的点都相连);
- p!=k,输出"Yes";
- 不是,输出"Not a Clique";
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int g[256][256];
int main()
{
int n,m;
cin>>n>>m;
int u, v;
memset(g, 0, sizeof(g));
while(m--){
cin>>u>>v;
g[u][v] = g[v][u] = 1;
g[u][0]++;//g[][0]哨兵,相连结点的个数,即边数
g[v][0]++;
}
int q;
cin>>q;
while(q--){
int k;
cin>>k;
int a[k+3];
for(int i=0; i<k; i++){
cin>>a[i];
}
bool flag = true;
int p = 0;//记录边数超过k-1的结点的数目
for(int i=0; i<k; i++){
if(g[a[i]][0]<k-1){//边数小于k-1一定不满足Clique
flag = false;
break;
}
for(int j=0; j<k; j++){//对每一个结点判断是否连接了其他剩余结点
if(!g[a[i]][a[j]] && a[i] != a[j]){
flag = false;
break;
}
}
if(g[a[i]][0]>k-1) p++;
}
if(flag){
if(p==k){//若所有结点的边数都超过k-1,进一步判断是否能构成Maximal Clique
bool ff = true;
for(int i=1; i<=n; i++){
int q = 0;
for(int j=0; j<k; j++){
if(g[a[j]][i])
q++;
}
if(q == k){//存在点满足与其他点两两连接
printf("Not Maximal\n");
ff = false;
break;
}
}
if(ff) printf("Yes\n");
} else printf("Yes\n");
} else{
printf("Not a Clique\n");
}
}
return 0;
}