原题链接:1142 Maximal Clique (25分)
关键词:图论、枚举
整理时间:2020.7.24
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
题目大意: 一个团是指这其中的任意两个顶点都是连通的,最大团是指不能再往里面加顶点构成团。现在给出一个图和若干个询问,问你询问的结点是否是团、或者最大团。
分析:设置两个bool型变量用于判断是否是团和是否是最大团。
- 如果存在两个顶点不相连:不是团;
- 是团的话,看图里的其他结点,如果存在一个结点与团中的结点都相连:不是最大团;
- 否则:最大团
代码:
#include <iostream>
#include <vector>
#include <set>
using namespace std;
const int maxn = 210;
int nv, ne, m, k; //v无向图中点数量 e边的数量 m询问数量 k子集包含点的数量
int g[maxn][maxn] = {0};
int main(){
cin >> nv >> ne;
for(int i = 0; i < ne; i ++ ){
int a, b;
cin >> a >> b;
g[a][b] = g[b][a] = 1;
}
cin >> m; //m个询问
for(int i = 0; i < m; i ++ ){
cin >> k; //k个结点
bool is_cli = true, is_max = true;
vector<int> v(k);
set<int> vis; // 出现过的点
for(int i = 0; i < k; i++){
cin >> v[i];
vis.insert(v[i]);
}
for(int i = 0; i < k - 1; i++){
for(int j = i + 1; j < k; j++){
if(g[v[i]][v[j]] != 1){ //不连通不是团
is_cli = false;
puts("Not a Clique");
break; // 跳出内循环了
}
}
if(is_cli == false)
break; //跳出外循环
}
if(is_cli == false) continue; //进行下一组询问
// 查看所有的点,看是否存在一个点满足和集合中所有的结点连接
for(int i = 1; i <= nv; i++){
if(!vis.count(i)){ //i是图里的其他点
int j;
for(j = 0; j < k; j++){
if(g[i][v[j]] == 0)
break;
}
if(j == k){
is_max = false;
break;
}
}
}
if(is_max) puts("Yes");
else puts("Not Maximal");
}
return 0;
}