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.
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.
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.
- 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
- Yes
- Yes
- Yes
- Yes
- Not Maximal
- Not a Clique
这道题首先在于看懂题目,判断输入的图是否是一个两两连通的团,或者是否是极大团,最后总结归纳为以下两个条件:
- 是否两两相连
- 是否存在每个人都有的共同邻居
然后分别定义两个判断上述条件的bool类型函数,再根据要求在输出时进行判断
- #include
- using namespace std;
- const int maxn = 210;
- int Nv, Ne, v1, v2, m;
- int edge[maxn][maxn] = { 0 };//邻接矩阵
- bool isconnected(vector<int> vlist) {
- //判断是否两两相连
- bool flag = true;
- for (int i = 0; i < vlist.size()-1; i++) {
- for (int j = i + 1; j < vlist.size(); j++) {
- if (edge[vlist[i]][vlist[j]] != 1) {
- flag = false;
- return flag;
- }
- }
- }
- return flag;
- }
- bool addNeighbour(vector<int> vlist) {
- bool flag = false;//true可以加邻居
- for (int i = 1; i <= Nv; i++) {
- if (find(vlist.begin(), vlist.end(), i) == vlist.end()) {//该节点不在查询队列里
- for (int j = 0; j < vlist.size(); j++) {
- if (edge[vlist[j]][i] != 1) {
- flag = false;
- break;
- }
- else flag = true;
- }
- }
- if (flag)break;
- }
- return flag;
- }
- int main()
- {
- cin >> Nv >> Ne;
- //输入
- for (int i = 0; i < Ne; i++) {
- cin >> v1 >> v2;
- edge[v1][v2] = edge[v2][v1] = 1;
- }
- //查询,判断是否是极大团
- //1、是否两两相连
- //2、是否存在每个人都有的共同邻居
- cin >> m;
- int num,v;
- for (int i = 0; i < m; i++) {
- cin >> num;
- vector<int> querylist(num);
- for (int j = 0; j < num; j++) {
- cin >> querylist[j];
- }
- sort(querylist.begin(), querylist.end());
- if (isconnected(querylist)) {
- if(!addNeighbour(querylist))cout << "Yes" << endl;
- else cout << "Not Maximal" << endl;;
- }
- else cout << "Not a Clique" << endl;
- }
- return 0;
- }
