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
团:团内点点相连;最大团:团外加一点 整个团就不点点相连
用一个邻接矩阵保存图信息,有边即为1,用数组v保存待定团,判断团内是否相连 ; 用otherorder数组保存待定团以外的顶点,判断团内顶点与团外顶点相连边数,为团内顶点数,即团外有点与团内点都相连,团不是最大团。
#include <stdio.h>
#include <vector>
using namespace std;
int main()
{
int nv,ne,m;
scanf("%d %d",&nv,&ne);
int graph[201][201];
for(int i=1;i<=nv;i++)
{
for(int j=1;j<=nv;j++)
{
graph[i][j]=0;
graph[i][i]=1;
}
}
while(ne--)
{
int a,b;
scanf("%d %d",&a,&b);
graph[a][b]=graph[b][a]=1;
}
scanf("%d",&m);
while(m--)
{
int k;
scanf("%d",&k);
int v[nv];
//输入
for(int i=0;i<k;i++)
{
scanf("%d",&v[i]);
}
//剩余顶点
vector<int> otherOrder(nv+1);
//初始化
for(int oi=1;oi<=nv;oi++)
{
otherOrder[oi]=0;
}
//团判断--团内点点相连
int isClique=0;
for(int i=0;i<k;i++)
{
otherOrder[v[i]] ++;
for(int j=0;j<k;j++)
{
if(graph[v[i]][v[j]]==0){
isClique++;
}
}
}
//移除输入点的剩余顶点
int index=0;
int temp=1;
while(temp<=nv)
{
if(otherOrder[temp]>0)
{
index++;
}else{
otherOrder[temp-index]=temp;
}
temp++;
}
int isMaximal=0;
//最大团
for(int ir=1;ir<=nv-index;ir++)
{
int temp=otherOrder[ir];
int maximal=0;
for(int jr=0;jr<k;jr++)
{
//新添加的点若能跟团内点各各相连-该团不是最大团
if(graph[v[jr]][temp]==1){
maximal++;
}
}
if(maximal>isMaximal){
isMaximal=maximal;
}
}
//判断输出
if(isClique>0)
{
printf("Not a Clique\n");
}else{
if(isMaximal==k)
{
printf("Not Maximal\n");
}else{
printf("Yes\n");
}
}
}
return 0;
}