【PAT甲级】1142 Maximal Clique

简介: 【PAT甲级】1142 Maximal Clique

1142 Maximal Clique

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


题意

在一个无向图中,如果一个顶点子集满足子集内的任意两个不同顶点之间都是相连的,那么这个顶点子集就被称为一个团。


如果一个团不能通过加入某个新的顶点来扩展成一个更大的团,那么该团就被称为最大团。


现在,你需要判断给定顶点子集能否构成一个最大团。


举个例子,下图中 [1,2,3,4] 就是该图的最大团,而 5 不在其中是因为 5 与 3 和 2 没有边。

思路

1.这题用邻接矩阵来存储每一条边。

2.每次询问时用数组 vers 去存储每个点值,并用 cnt 记录点数。然后先判判断该子集是否是团,如果是团再进一步判断是否为最大团。

1.判断是否是团,只需判断子集中每个点之间是否都存在边。

2.判断是否为最大团,需要先用一个数组 st 来标记子集中有哪些点,然后再遍历不是子集中的点,再去判断这些点是否和子集中的每个点都存在一条边,只要能找到一个这种点,就说明不是最大团。

3.根据判断结果,输出对应语句。


代码

#include<bits/stdc++.h>
using namespace std;
const int N = 210;
int g[N][N];
int vers[N];
bool st[N];
int n, m;
//判断是不是团
bool check_clique(int cnt)
{
    //判断每个点之间是否都存在边
    for (int i = 0; i < cnt; i++)
        for (int j = 0; j < i; j++)
            if (!g[vers[i]][vers[j]])
                return false;
    return true;
}
//判断是不是最大团
bool check_maximum(int cnt)
{
    memset(st, 0, sizeof st); //初始化
    //先将子集中的点进行标记
    for (int i = 0; i < cnt; i++)  st[vers[i]] = true;
    //然后遍历不在子集中的点
    for (int i = 1; i <= n; i++)
        if (!st[i])
        {
            //判断该点是否与子集中的所有点都有边
            bool success = true;
            for (int j = 0; j < cnt; j++)
                if (!g[i][vers[j]])
                {
                    //只要子集中有一个点与其没边,该点就不属于该团
                    success = false;
                    break;
                }
            //根据遍历结果判断是否为最大团
            if (success) return false;
        }
    return true;
}
int main()
{
    cin >> n >> m;
    //记录每条边
    for (int i = 0; i < m; i++)
    {
        int a, b;
        cin >> a >> b;
        g[a][b] = g[b][a] = true;
    }
    //开始查询
    int k;
    cin >> k;
    while (k--)
    {
        int cnt;
        cin >> cnt;
        for (int i = 0; i < cnt; i++)    cin >> vers[i];   //输入子集
        //判断是不是团
        if (check_clique(cnt))
        {
            //判断是不是最大团
            if (check_maximum(cnt))  puts("Yes");
            else    puts("Not Maximal");
        }
        else    puts("Not a Clique");
    }
    return 0;
}


目录
相关文章
HDU-2566,统计硬币(暴力 or DP)
HDU-2566,统计硬币(暴力 or DP)
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17120 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
6天前
|
云安全 数据采集 人工智能
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1284 3
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2356 7
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3831 22