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
题目给出若干子图顶点,要求判断这个子图是不是clique,如果是,它是不是最大 clique,也就是说,图中还有顶点与该子图中的顶点都有相邻关系
这题我用到了邻接矩阵和邻接表,邻接矩阵用来判断两个顶点矩阵之间是否有相邻关系
邻接表用来判断子图外的顶点是否和子图都有相邻关系
首先在子图内进行判断,也就是子图顶点之间两两比较,排除掉非clique的情况
确认是clique后,判断是否是max clique
我们利用哈希表把子图内的顶点标记,然后把子图所有顶点的邻接顶点存到一个set中,然后将这些结点与子图内的顶点二二比较,如果存在一个顶点与子图中的顶点都有邻接关系,那么这个子图就不是maxclique
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <string.h>
#include <set>
using namespace std;
const int MANX = 210;
int N, M, K;
int BBedges[MANX][MANX];
vector<int> EDGEs[MANX];
bool hashTAble[MANX] = { false };
int main() {
cin >> N >> M;
int nx, ny;
for (int i = 0; i < M; ++i) {
cin >> nx >> ny; //无向边
EDGEs[nx].push_back(ny);
EDGEs[ny].push_back(nx);
BBedges[nx][ny] = 1;
BBedges[ny][nx] = 1;
}
cin >> K;
for (int i = 0; i < K; ++i) {
vector<int> nodesTemp;
int nums,xnode;
cin >> nums;
for (int j = 0; j < nums; ++j) {
cin >> xnode;
nodesTemp.push_back(xnode);
hashTAble[xnode] = true;
}
//第一轮:是不是clique
bool isclque = true;
for (auto x : nodesTemp) {
for (auto y :nodesTemp) {
if(x == y) continue;
if (BBedges[x][y] != 1) {
isclque = false;
break;
}
}
if(!isclque) break;
}
//第二轮:是不是maxclique
if (isclque) {
set<int> nodesSet;
for (auto x : nodesTemp) {
for (auto y : EDGEs[x]) {
nodesSet.insert(y);
}
}
bool notMax = true;
for (auto z : nodesSet) {
if (!hashTAble[z]) {
bool ismax = true;
for (auto a : nodesTemp) {
if(a == z) continue;
if (BBedges[z][a] != 1) {
ismax = false;
break;
}
}
if (ismax) {
cout << "Not Maximal" << endl;
notMax = false;
break;
}
}
}
if (notMax) {
cout << "Yes" << endl;
}
}
else {
cout << "Not a Clique" << endl;
}
memset(hashTAble, 0, MANX);
}
system("PAUSE");
return 0;
}