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 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)如果该数组内的点之间不是两两直接连接,输出Not a Clique               2)如果1)为真,判断给出的数组是否是最大,如果是输出Yes,不是  输出Not Maximal。  根据题意使用邻接矩阵进行存储图(这样判断两个顶点之间是否直接连接很方便)。
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<string.h>
#include<cstdio>
#include<cmath>
using namespace std;

int graph[201][201];

void judge(int a[],int k,int n)
{
    bool visited[n+1];
    fill(visited,visited+n+1,true);

    for(int i=0;i<k;i++)
    {

        int temp1=a[i];
        visited[temp1]=false;
        for(int j=i+1;j<k;j++)
        {
            int temp2=a[j];
            if(graph[temp1][temp2]==0)
            {
                printf("Not a Clique\n");
                return;
            }
        }
    }
    for(int i=1;i<n+1;i++)
    {
        if(visited[i])
        {
            bool flag=true;
            for(int j=0;j<k;j++)
            {
                if(graph[i][a[j]]==0)
                    flag=false;
            }
            if(flag)
            {
                printf("Not Maximal\n");
                return;
            }
        }
    }

    printf("Yes\n");

}

int main()
{
    int n,m;
    cin>>n>>m;
   // graph.resize(n+1);
    for(int i=0;i<m;i++)
    {
        int start,endL;
        cin>>start>>endL;
        //graph[start].push_back(start);
        graph[start][endL]=1;
        graph[endL][start]=1;
       // graph[endL].push_back(endL);
    }
    cin>>m;
    for(int i=0;i<m;i++)
    {
        int k;
        cin>>k;
        int temp[k];
        for(int j=0;j<k;j++)
        {
           int a;
           cin>>a;
            temp[j]=a;
        }
        judge(temp,k,n);
    }

    return 0;
}
 
 

原文地址:https://www.cnblogs.com/zhanghaijie/p/10323421.html

时间: 2024-08-07 03:13:40

1142 Maximal Clique (25 分)图的相关文章

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.or

PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any oth

4-9 二叉树的遍历 (25分)

4-9 二叉树的遍历   (25分) 输出样例(对于图中给出的树): Inorder: D B E F A G H C I Preorder: A B D F E C G H I Postorder: D E F B H G I C A Levelorder: A B C D F G I E H 代码:(都是遍历的算法) 1 // 4-9 二叉树的遍历 2 // 3 // Created by Haoyu Guo on 04/02/2017. 4 // Copyright ? 2017 Haoy

5-3 树的同构 (25分)

5-3 树的同构   (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树.而图2就不是同构的. 图1 图2 现给定两棵树,请你判断它们是否是同构的. 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数NN (\le 10≤10),即该树的结点数(此时假设结点从0到N-1N?1编号):随后NN行,第i

PTA - - 06-图1 列出连通集 (25分)

06-图1 列出连通集   (25分) 给定一个有NN个顶点和EE条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N-1N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点. 输入格式: 输入第1行给出2个整数NN(0<N\le 100<N≤10)和EE,分别是图的顶点数和边数.随后EE行,每行给出一条边的两个端点.每行中的数字之间用1空格分隔. 输出格式: 按照"{ v_1v?1?? v_2v?2?? ... v_kv?k?? 

PTA 树的同构(25 分)

7-1 树的同构(25 分) 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树.而图2就不是同构的. 图1 图2 现给定两棵树,请你判断它们是否是同构的. 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N?1编号):随后N行,第i行对应编号第i个结点,给出

PTA 旅游规划(25 分)

7-10 旅游规划(25 分) 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条路径都是最短的,那么需要输出最便宜的一条路径. 输入格式: 输入说明:输入数据的第1行给出4个正整数N.M.S.D,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0~(N?1):M是高速公路的条数:S是出发地的城市编号:D是目的地的城市编号.随后的M行中,每行给出一条高速公路的信息,分别

06-图1 列出连通集 (25 分)

06-图1 列出连通集 (25 分) 给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N?1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点. 输入格式: 输入第1行给出2个整数N(0<N≤10)和E,分别是图的顶点数和边数.随后E行,每行给出一条边的两个端点.每行中的数字之间用1空格分隔. 输出格式: 按照"{ v?1?? v?2?? ... v?k?? }"的格式,每行输出一个连通集.先输出DFS的结果

7-1 列出连通集 (25 分)

7-1 列出连通集 (25 分) 给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N?1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点. 输入格式: 输入第1行给出2个整数N(0<N≤10)和E,分别是图的顶点数和边数.随后E行,每行给出一条边的两个端点.每行中的数字之间用1空格分隔. 输出格式: 按照"{ v?1?? v?2?? ... v?k?? }"的格式,每行输出一个连通集.先输出DFS的结果,再