本题链接:1142 Maximal Clique - PAT (Advanced Level) Practice (pintia.cn)
题意:
给定clique的定义,判断给出的点集是否满足要求
先判断是否为clique,在判断是否为maximal
代码:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int mp[210][210];
vector<int> nums;
int nv, ne, m, k;
bool chargeCli() {
for (int i = 0; i < nums.size(); i++) {
for (int j = i + 1; j < nums.size(); j++) {
if (mp[nums[i]][nums[j]] == 0) {
return false;
}
}
}
return true;
}
bool chargeMax() {
bool vis[210];
fill(vis, vis + 210, false);
for (int i = 0; i < nums.size(); i++) {
vis[nums[i]] = true;
}
for (int j = 1; j <= nv; j++) {
if (!vis[j]) {
bool ans = true;
for (int i = 0; i < nums.size(); i++) {
if (!mp[nums[i]][j]) {
ans = false;
}
}
if (ans) {
return false;
}
}
}
return true;
}
//clique是无向图中每个点都相邻
//max clique是无法再添加临近点
int main() {
for (int i = 0; i < 210; i++) {
for (int j = 0; j < 210; j++) {
if (i == j) {
mp[i][j] = 1;
}
else {
mp[i][j] = 0;
}
}
}
cin >> nv >> ne;
for (int i = 0; i < ne; i++) {
int a, b;
cin >> a >> b;
mp[a][b] = mp[b][a] = 1;
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> k;
if (nums.size()) {
nums.clear();
}
for (int j = 0; j < k; j++) {
int a;
cin >> a;
nums.push_back(a);
}
if (!chargeCli()) {
cout << "Not a Clique" << endl;
}
else {
if (!chargeMax()) {
cout << "Not Maximal" << endl;
}
else {
cout << "Yes" << endl;
}
}
}
return 0;
}