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
最近的题目经常看不懂,怪不得我过不了cet6,判断给你的这些结点是否为clique,再判断是否为maxclique,clique就是点与点之间都有边相连,maxClique就是除了这几个点外,这张图里没有多余的点也和这几个点均有连接,判断max,就用一个数组记录要判断的set的里的点,然后遍历图的结点,如果这个点不是set里的点,就判断它是否和set里每个点都有连接,如果有一个没连接,就break,如果遍历到最后都没break,证明这个点可以加入set中,证明这个set不是max的
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int G[205][205];
bool vis[205] = {false};
int main(){
int Nv,Ne, u, v;
cin>>Nv>>Ne;
fill(G[0] , G[0] + 205 * 205, 0);
for(int i = 0; i < Ne; i++){
cin>>u>>v;
G[u][v] = G[v][u] = 1;
}
int m,k,x;
cin>>m;
for(int i = 0 ; i < m; i++){
cin>>k;
vector<int> v;
bool isClique = true, isMax = true;
fill(vis,vis+205,false);
for(int j = 0; j < k; j++){
cin>>x;
v.push_back(x);
vis[x] = true;
}
for(int j = 0; j < k; j++){
for(int l = j +1; l < k; l++){
if(G[v[j]][v[l]] == 0) isClique = false;
break;
}
if(isClique == false) break;
}
if(isClique == false){
cout<<"Not a Clique\n";
continue;
}
for(int j = 1; j <= Nv; j++){
if(vis[j] == false){
for(int l = 0; l < k; l++){
if(G[j][v[l]] == 0) break;
if(l==k-1) isMax = false;
}
if(isMax == false){
cout<<"Not Maximal"<<endl;
break;
}
}
}
if(isMax == true) cout<<"Yes\n";
}
return 0;
}
二刷,上课做的,其实只要看懂题目了就不难。代码写的一般吧,和上次55开。判断是否还有点和点集中的所有点都有通路,如果有就不是最大的,如果不连通or不是圆直接not a clique
//判断是否是clique,双重for判断每两个点之间是否连通
//判断是否最大,从1到n,如果还有一个点能和其中所有点都有通路,那就不是最大的
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 205;
int G[maxn][maxn];
int main(){
fill(G[0],G[0]+maxn*maxn,0);
int n,m;
cin>>n>>m;
int u,v;
for(int i = 0; i < m; i++){
cin>>u>>v;
G[u][v] = G[v][u] = 1;
}
int q,k,dian;
cin>>q;
for(int i = 0; i < q; i++){
cin>>k;
vector<int> v;
bool isClique = true;
bool isMax = true;
for(int j = 0; j < k; j++){
cin>>dian;
v.push_back(dian);
}
for(int j = 0; j < k-1; j++){
for(int l = j + 1; l < k; l++){
if(G[v[j]][v[l]] != 1){
isClique = false;
break;
}
}
if(isClique == false) break;
}
for(int j = 1; j <= n; j++){
bool isM = false;;
for(int l = 0; l < k; l++){
if(G[j][v[l]] != 1) isM = true;
}
if(isM == true) isMax = true;
else{
isMax = false;
break;
}
}
if(isClique && isMax) cout<<"Yes\n";
else if(isClique && !isMax) cout<<"Not Maximal\n";
else if(!isClique) cout<<"Not a Clique\n";
}
return 0;
}
//先判断是否是clique,两两之间看看是否有通路,如果有一个没有直接flase
//遍历所有结点,如果有一个结点和所有点集内的点都有通路,max就是flase。最后max和clique才是true
#include<vector>
#include<iostream>
using namespace std;
const int maxn = 205;
int G[maxn][maxn];
int main(){
int n,m;
cin>>n>>m;
int u,v;
for(int i = 0 ; i < m; i++){
cin>>u>>v;
G[u][v] = G[v][u] = 1;
}
int q,k;
cin>>q;
vector<int> vertex;
for(int i = 0; i< q; i++){
cin>>k;
vertex.resize(k+1);
bool isClique = true;
bool isMax = true;
for(int j = 0;j < k; j++){
cin>>vertex[j];
}
for(int j = 0; j < k-1; j++){
for(int l = j + 1; l < k; l++){
if(G[vertex[j]][vertex[l]] == 0){
isClique = false;
break;
}
}
if(!isClique) break;
}
if(!isClique){
cout<<"Not a Clique\n";
continue;
}
for(int j = 1; j <= n; j++){
bool isM = false;
for(int l = 0; l < k; l++){
if(G[j][vertex[l]] == 0){
isM = true;
break;
}
}
if(isM == false) isMax = false;
}
if(!isMax) cout<<"Not Maximal\n";
else cout<<"Yes\n";
}
return 0;
}