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

 #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <set>
#include <cctype>
using namespace std;
const int maxn=;
int n,m,k;
set<int> adj[maxn];
int g[maxn][maxn];
int main(){
fill(g[],g[]+maxn*maxn,);
scanf("%d %d",&n,&m);
for(int i=;i<m;i++){
int c1,c2;
scanf("%d %d",&c1,&c2);
g[c1][c2]=;
g[c2][c1]=;
adj[c1].insert(c2);
adj[c2].insert(c1);
}
scanf("%d",&k);
for(int i=;i<k;i++){
int x;
scanf("%d",&x);
int que[maxn]={};
int vis[maxn]={};
for(int j=;j<x;j++){
int tmp;
scanf("%d",&tmp);
que[j]=tmp;
vis[tmp]=;
}
int flag=;
for(int j=;j<x;j++){
if(flag==){
for(int q=j+;q<x;q++){
if(g[que[j]][que[q]]==){
flag=;
printf("Not a Clique\n");
break;
}
}
}
else break;
}
if(flag==){
int flag1=,flag2=;
for(int j=;j<=n;j++){
flag2=;
if(vis[j]==) continue;
for(int q=;q<x;q++){
if(g[que[q]][j]==){
flag2=;
break;
}
}
if(flag2==){
flag1=;
printf("Not Maximal\n");
break;
}
}
if(flag1==)printf("Yes\n");
}
}
}

注意点:题目意思就是找一个团,里面每个元素之间都两两相连,如果没有另外的点可以加进去后还满足这个条件就是最大团。一开始一直在想把给定序列的元素每个遍历,找到每个元素相连的元素,再去对他们做交集,看看和给定序列一不一样。做交集这个就很麻烦了,看了大佬的思路,原来不用纠结在给定的点上,要看另外的点能不能加进来,其实就是题目里给的意思,我自己转弯了。

这种多个判断的题已经不是第一次做到了,类似的还有1150 Travelling Salesman Problem (25 分),也是和图有关,判断各种东西

PAT A1142 Maximal Clique (25 分)——图的更多相关文章

  1. PTA PAT排名汇总(25 分)

    PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...

  2. [PAT] 1142 Maximal Clique(25 分)

    1142 Maximal Clique(25 分) A clique is a subset of vertices of an undirected graph such that every tw ...

  3. PAT 1142 Maximal Clique[难]

    1142 Maximal Clique (25 分) A clique is a subset of vertices of an undirected graph such that every t ...

  4. A1142. Maximal Clique

    A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the ...

  5. PAT 1142 Maximal Clique

    A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the ...

  6. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  7. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

  8. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  9. PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

随机推荐

  1. Java坦克大战(三)

    关于这个坦克大战的项目是在学习Java基础的时候,拿来练习的最近看到这些代码,感觉很亲切,就把他们都复制下来,编辑成博客.回首看去,Java基础的学习确实应该建立在找项目练习上,这样才能将学到的基础知 ...

  2. Centos7 firewalld 基本使用

    Centos7 的防火墙 firewalld比较常见 简单介绍使用 详细介绍链接推荐:   https://blog.csdn.net/buster_zr/article/details/806049 ...

  3. 设计模式之策略模式(Strategy)

    策略模式将不同算法的逻辑抽象接口封装到一个类中,通过组合和多态结合的方式来进行不同算法具体的实现. 作用 策略模式是一种定义一系列算法的方法,Strategy类层次为Context定义了一系列的可重用 ...

  4. JS之this应用详解

    目录 1. this作为全局变量2. 作为对象方法的调用3. 作为构造函数调用4. apply调用 this是Javascript语言的一个关键字.它代表函数运行时,自动生成的一个内部对象,只能在函数 ...

  5. 2017-12-05 JavaScript实现ZLOGO子集: 前进+转向

    在前文中文编程语言之Z语言初尝试: ZLOGO 4与相关讨论后, 萌生了用JavaScript编写类似语言以便在线编程的想法. 于是使用 @TKT2016 (知乎账号)的ZLOGO语法设计, 在编程语 ...

  6. 贝塞尔曲线UIBezierPath简单使用

    //常用属性 /* 1.CGPath: 将UIBezierPath类转换成CGPath 2.currentPoint: 当前path的位置,可以理解为path的终点 3.lineWidth: 线条宽度 ...

  7. Expo大作战(三十六)--expo sdk api之 ImagePicker,ImageManipulator,Camera

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  8. Java:JDBC的基本使用

    本文内容: 什么是JDBC JDBC的使用 事务 连接池 DbUtils 首发日期:2018-05-27 修改: 2018-07-19:增加了事务.连接池.DBUtils 2018-07-27:对特别 ...

  9. 【redis专题(4)】命令语法介绍之sorted_set

    有序集合可以模拟优先级队列的实现 增 zadd key score1 value1 score2 value2 .. redis 127.0.0.1:6379> zadd stu 18 lily ...

  10. Excel的快速录入

    数据有效性: 1.选择要限制数据有效性的区域: 2.点开[数据]选项卡选择”数据验证“: 3.[设置]中选择”序列": 4.若手动输入则需要将内容使用英文符号分割开来(比如:A级,B级): ...