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
题目大意:在一个无向图中,如果一个点集中任意两个不同的点之间都是相连的,则称为clique(团)。maximal clique表示最大团,即除了团里的节点之外,不存在任何一个新的结点加入团后还能满足团的要求。现有1~nv共nv个点,ne条边。之后m次询问,每个询问是一个点集合,问这个点集合是否是maximal clique、是否是clique。
分析:先判断是否是clique:检查是否集合中任意两点都有边。若是,则判断是否是maximal:遍历所有不在集合中的剩余的点,看是否存在一个点满足和集合中所有的结点相连。
#include<algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;
int main(void)
{
#ifdef test
freopen("in.txt","r",stdin);
//freopen("in.txt","w",stdout);
clock_t start=clock();
#endif //test
int nv,ne;scanf("%d%d",&nv,&ne);
int num[nv+5][nv+5];
for(int i=1;i<=nv;++i)
for(int j=1;j<=nv;++j)
num[i][j]=0;
for(int i=0;i<ne;++i)
{
int a,b;scanf("%d%d",&a,&b);
num[a][b]=num[b][a]=1;
num[a][0]++,num[b][0]++;
}
// for(int i=1;i<=nv;++i)
// {
// printf("i=%d %d",i,num[i][0]);
// for(int j=1;j<=nv;++j)
// if(num[i][j]==1)printf(" %d",j);
// printf("\n");
// }printf("\n");
int k;scanf("%d",&k);
while(k--)
{
int t,f=1;scanf("%d",&t);
int ques[t+5]={0},flag[nv+5]={0};
for(int i=0;i<t;++i)
{
scanf("%d",&ques[i]);flag[ques[i]]=1;
if(f)
for(int j=0;j<i;++j)
{
if(num[ques[i]][ques[j]]==0)f=0;
if(!f)break;
}
}
if(f==0)printf("Not a Clique\n");
else
{
int f1=1,cnt=0;
for(int i=1;i<=nv;++i)
{
f1=1;
if(!flag[i])
{
for(int j=0;j<t;++j)
{
if(num[i][ques[j]]==0)
{
f1=0;break;
}
}
if(f1)cnt++;
}
}
if(cnt)printf("Not Maximal\n");
else printf("Yes\n");
}
}
#ifdef test
clockid_t end=clock();
double endtime=(double)(end-start)/CLOCKS_PER_SEC;
printf("\n\n\n\n\n");
cout<<"Total time:"<<endtime<<"s"<<endl; //s为单位
cout<<"Total time:"<<endtime*1000<<"ms"<<endl; //ms为单位
#endif //test
return 0;
}