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
题目大意:给一个图,再给出一些点,判断这些点是否两两相通,同时判断是否还在其他的点与这些点两两相通。
解题思路:是否两两相通这个设一个数组,记录所有的路径即可,是否还存在其他的点,用set容器记录第一个点所有的邻边点,判断这些邻边点与剩下的点是否两两相通即可。重点留意一下自身点的路径记录以及相通判断。
Tips:
这题纠正了我erase上认识的不足;
for(auto it=s.begin();it!=s.end();it++){
if(e[*it][temp[v]]==false)
s.erase(it);
}
以上代码是错的,对任意一个容器都不正确。
原因:可以参考这篇文章https://www.techiedelight.com/remove-elements-set-cpp/
简单来说,就是当执行了erase之后,返回一个迭代器其实是指向被删除元素的迭代器的后一个,如果按照上述处理方式,it指向的被删除的元素,其实是不明确的,上文给了几种解决方式,我也简单地写一下:
//方法一:复制一个指向需要删除的元素的后一个迭代器
for(auto it = s.begin(); it != s.end()){
if(*it < target){
auto dup = it ++;
s.erase(it);
it = dup;
}
else
it ++;
}
//方法二:提前将it指向下一个
for(auto it = s.begin(); it != s.end()){
if(*it < target)
s.erase(it ++);
else
it ++;
}
//方法三:利用erase返回被删除的元素的后一个迭代器
for(auto it = s.begin(); it != s.end()){
if(*it < target)
it = s.erase(it);
else
it ++;
}
#include <iostream>
#include <set>
#include <vector>
using namespace std;
bool e[205][205];
int n, m, k, cnt;
int main(){
scanf("%d %d", &n, &m);
for(int i = 0; i < m; ++ i){
int a, b;
scanf("%d %d", &a, &b);
e[a][b] = e[b][a] = true;
}
scanf("%d", &k);
for(int i = 0; i < k; ++ i){
scanf("%d", &cnt);
set<int>ans;
vector<int>val(cnt);
bool flag = true;
for(int j = 0; j < cnt; ++ j)
scanf("%d", &val[j]);
ans.insert(val[0]);
for(int j = 1; j <= n; ++ j)
if(e[val[0]][j] == true)
ans.insert(j);
for(int j = 1; j < cnt; ++ j){
if(ans.find(val[j]) == ans.end()){
flag = false;
break;
}
for(auto it = ans.begin(); it != ans.end();)
if(*it != val[j] && e[*it][val[j]] == false)
ans.erase(it ++);
else
it ++;
}
printf("%s\n",flag == false ? "Not a Clique" : ans.size() != cnt ? "Not Maximal" : "Yes");
}
}