PAT 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

#include<iostream>
#include<vector>
using namespace std;
int main(){
    int nv, ne, k, n;
    cin>>nv>>ne;
    vector<vector<int>> G(205, vector<int>(205, 0));
    for(int i=0; i<ne; i++){
        int v1, v2;
        cin>>v1>>v2;
        G[v1][v2]=G[v2][v1]=1;
    }
    cin>>k;
    for(int i=0; i<k; i++){
        bool full=true, clique=true;
        cin>>n;
        vector<int> vi(n, 0), a(nv+1, 0);
        for(int j=0; j<n; j++){
            cin>>vi[j];
            a[vi[j]]=1;
        }
        for(int j=0; j<n; j++){
            if(clique==false) break;
            for(int l=j+1; l<n; l++){
                if(G[vi[j]][vi[l]]!=1){
                    clique=false;
                    cout<<"Not a Clique"<<endl;
                    break;
                }
            }
        }
        if(clique==false) continue;
        for(int j=1; j<=200; j++){
            if(a[j]==0){
                for(int l=0; l<n; l++){
                    if(G[vi[l]][j]==0)  break;
                    if(l==n-1) full=false;
                }
            }
            if(!full){
                cout<<"Not Maximal"<<endl;
                break;
            }
        }
            if(full) cout<<"Yes"<<endl;
    }
    return 0;
} 

原文地址:https://www.cnblogs.com/A-Little-Nut/p/9652266.html

时间: 2024-11-11 05:35:26

PAT 1142 Maximal Clique的相关文章

1142 Maximal Clique (25 分)图

1142 Maximal Clique (25 分) 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 f

PTA 1140 1141 1142 1143

1140 Look-and-say Sequence 思路:模拟 #include<bits/stdc++.h> using namespace std; typedef long long ll; int d,n; const int maxn = 10010; int cnt[11]; vector<int> a; vector<int> b; int main(){ scanf("%d%d",&d,&n); a.push_bac

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

Markov Random Fields

We have seen that directed graphical models specify a factorization of the joint distribution over a set of variables into a product of local conditional distributions. They also define a set of conditional independence properties that must be satisf

七月算法-12月机器学习在线班--第十八次课笔记-条件随机场CRF

七月算法-12月机器学习在线班--第十八次课笔记-条件随机场CRF 七月算法(julyedu.com)12月机器学习在线班学习笔记http://www.julyedu.com 1,对数线性模型 一个事件的几率odds,是指该事件发生的概率与该事件不发生的概率的比值. 1.1对数线性模型的一般形式 令x为某样本,y是x的可能标记,将Logistic/ Softmax回归的特征 记做 特征函数的选择:eg: 自然语言处理 1, 特征函数几乎可任意选择,甚至特征函数间重叠: 2, 每个特征之和当前的词

用 Python 通过马尔可夫随机场(MRF)与 Ising Model 进行二值图降噪

前言 这个降噪的模型来自 Christopher M. Bishop 的 Pattern Recognition And Machine Learning (就是神书 PRML……),问题是如何对一个添加了一定椒盐噪声(Salt-and-pepper Noise)(假设噪声比例不超过 10%)的二值图(Binary Image)去噪. 原图 添加 10% 椒盐噪声的图 建模 下文中的数学表示: yi:噪声图中的像素 xi:原图中的像素,对应噪声图中的 yi 既然噪声图是从原图添加噪声而来,我们拥

算法大讲堂之二分图

二分图大讲堂——彻底搞定最大匹配数(最小覆盖数).最大独立数.最小路径覆盖.带权最优匹配 文本内容框架: §1图论点.边集和二分图的相关概念和性质 §2二分图最大匹配求解 匈牙利算法.Hopcroft-Karp算法 §3二分图最小覆盖集和最大独立集的构造 §4二分图最小路径覆盖求解 §5二分图带权最优匹配求解 Kuhn-Munkers算法 §6小结 每章节都详细地讲解了问题介绍,算法原理和分析,算法流程,算法实现四部分内容,力求彻底解决问题. §1图论点.边集和二分图的相关概念和性质 点覆盖.最

统计学习方法 李航---第11章 条件随机场

第11章 条件随机场 条件随机场(conditional random field, CRF)是给定一组输入随机变量条件下另一组输出随机变量的条件概率分布模型,其特点是假设输出随机变量构成马尔可夫随机场.条件随机场可以用于不同的预测问题,本章主要讲述线性链(linear chain)条件随机场在标注问题的应用,这时问题变成了由输入序列对输出序列预测的判别模型,形式为对数线性模型,其学习方法通常是极大似然估计或正则化的极大似然估计. 11.1 概率无向图模型 概率无向图模型(probabilist

NLP —— 图模型(二)条件随机场(Conditional random field,CRF)

本文简单整理了以下内容: (一)马尔可夫随机场(Markov random field,无向图模型)简单回顾 (二)条件随机场(Conditional random field,CRF) 这篇写的非常浅,基于 [1] 和 [5] 梳理.感觉 [1] 的讲解很适合完全不知道什么是CRF的人来入门.如果有需要深入理解CRF的需求的话,还是应该仔细读一下几个英文的tutorial,比如 [4] . (一)马尔可夫随机场简单回顾 概率图模型(Probabilistic graphical model,P