• pat 1142


    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

    题意:给定一个无向图,给出k个查询,每次查询给出num个顶点,要求判断在这个图中这些顶点是否两两相连,若不是,输出Not a Clique。若是,进一步判断往这个顶点集中再加入一个图中的其他顶点是否仍然满足条件,若 是,输出Not Maximal,否则输出Yes.

    思路:邻接矩阵存储图,对于每次查询,用一个vector数组保存顶点集。先判断这个顶点集中的顶点是否两两相连,再判断图中是否存在一个其他顶点与这个顶点集中的任意顶点都相连

    代码如下:

    #include<cstdio>
    #include<vector>
    #include<algorithm>
    #include<map>
    using namespace std;
    int nv,ne;
    bool G[205][205]={false};
    void judge(vector<int> v){
        map<int,bool> m2;
        map<int,int> m;
        for(int i=0;i<v.size();i++){
            m2[v[i]]=true;
        }
        for(int i=0;i<v.size();i++){
            for(int j=i+1;j<v.size();j++){
                if(G[v[i]][v[j]]==false||G[v[j]][v[i]]==false){
                    printf("Not a Clique
    ");
                    return ;
                }
            }
            for(int j=1;j<=nv;j++){
                if(G[v[i]][j]==true&&m2[j]==false){
                    if(m.count(j)==0){
                        m[j]=1;
                    }
                    else
                        m[j]=m[j]+1;
                }
            }
        }
        for(map<int,int>::iterator it=m.begin();it!=m.end();it++){
            if((*it).second==v.size()){
                printf("Not Maximal
    ");
                return;
            }
        }
        printf("Yes
    ");
    }
    int main(){
        int a,b;
        scanf("%d%d",&nv,&ne);
        for(int i=0;i<ne;i++){
            scanf("%d%d",&a,&b);
            G[a][b]=G[b][a]=true;
        }
        int k;
        int num;
        scanf("%d",&k);
        for(int i=0;i<k;i++){
            scanf("%d",&num);
            vector<int> v;
            int temp;
            for(int j=0;j<num;j++){
                scanf("%d",&temp);
                v.push_back(temp);
            }
            judge(v);
        }
        return 0;
    } 
  • 相关阅读:
    视觉slam十四讲课后习题ch3-7
    视觉slam十四讲课后习题ch3-6
    视觉slam十四讲课后习题ch3--5题
    (原创)遗传算法C++实现
    (笔记):组合and继承之访问限制(二)
    (笔记):组合and继承之访问限制(一)
    (笔记):初始化列表之初始化顺序
    (笔记):构造函数之初始化列表
    笔记:构造函数易错点
    (笔记):构造函数与析构函数
  • 原文地址:https://www.cnblogs.com/foodie-nils/p/13304920.html
Copyright © 2020-2023  润新知