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
Ω
给出一个无向图,判断其中的顶点子集是否为最大完全图,完全图指内部任意两个顶点之间均有边相连,而最大则指图中其余任意点的加入都不能再构成一个更大的完全图。
完全图只要遍历所有顶点对检查是否有边即可,而最大则检查剩余图中是否存在与子集中所有顶点均有边的顶点,若存在说明不是最大。
这种题用bitset就很爽,注意到顶点数不超过200个,因此我们给每一个顶点都开一个bitset<201>
,第
位为1代表与第
个顶点之间有边相连,那么201位就能代表一个顶点的连边情况。在读入一个顶点子集的过程中,每读入一个顶点,就将其连边情况与之前读入的顶点(也用bitset<201>
表示)进行并运算,如果结果与之前读入的顶点一致就代表该顶点与前面所有顶点之间均有边相连,否则说明不是完全图。最后将剩余顶点的连边情况与该顶点集的顶点进行并运算,如果结果等于顶点集中的顶点就说明不是最大的clique。
🐎
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int v, e, a, b;
cin >> v >> e;
bitset<201> edge[v + 1];
for (int i = 0; i < e; ++i)
{
cin >> a >> b;
edge[a][b] = edge[b][a] = true;
}
int n, m;
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> m;
bool ans = true;
vector<bool> flag(v + 1, false);
bitset<201> r(0);
for (int j = 0; j < m; ++j)
{
cin >> a;
if (!ans) continue;
if ((edge[a] & r) != r) ans = false;
r[a] = true, flag[a] = true;
}
if (!ans) printf("Not a Clique\n");
else
{
for (int j = 1; j <= v; ++j)
{
if (flag[j]) continue;
if ((edge[j] & r) == r)
{
ans = false;
break;
}
}
printf(ans ? "Yes\n" : "Not Maximal\n");
}
}
}