PAT甲級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

英文沒讀太懂,就畫了個圖猜了猜,應該是說互相連通了就是Clique比如 5634 和564,如果沒有其他的點添加進來構成新的互相連通,就是Maximal比如5634和278.
在這裏插入圖片描述

實現的話:判斷是否互相連通就是把給的邊都判斷一下,是否是互相有邊,比如給出了2 7 8三個點,判斷 2 7和2 8是否有邊,7 8 和7 2是否有邊 ,8 2和 8 7 是否有邊,其實能更簡便點,判斷27 ,28 ,78就好,我最初沒想到。然後對於是否是最大的,我是把所有給的點有邊的點記錄下來,比如2 有到3 7 8的邊,那麼就是sign[3]++ sign[7]++ sign[8]++, 加完之後判斷是否有sign[一個頂點]等於給的的頂點數(這裏2 7 8 的話就是3)


#include <iostream>
#include <string>
#include <vector>

using namespace std;
int edge[999][999];

int main()
{
    int Nv,Ne;
    cin>>Nv>>Ne;
    vector<int> vec[Nv+1];
    for(int i=0; i<Ne; i++)
    {
        int a=1,b=1;
        cin>>a>>b;
        edge[a][b]=1;
        edge[b][a]=1;
        vec[a].push_back(b);
        vec[b].push_back(a);
    }
    int num;
    cin>>num;
    while(num>0)
    {
        num--;
        int inNum;
        cin>>inNum;
        int sn[inNum];
        for(int i=0; i<inNum; i++)
            cin>>sn[i];
        int sign=1;
        for(int i=0; i<inNum&&sign; i++)
        {
            //對除了sn[i]之外的所有頂點測試是否有邊
            for(int m=0; m<inNum; m++)
            {
                if(m==i)
                    continue;
                if(edge[sn[i]][sn[m]]==0)
                {
                    sign=0;
                    break;
                }
            }
        }
        if(sign==1)
        {
            //判斷是否是最大的
            int MaxSign=1;
            int idSign[Nv+1]= {0};
            for(int m=0; m<inNum&&MaxSign; m++){
                for(int i=0; i<vec[sn[m]].size(); i++)
                {
                    int currentID=sn[m];
                    int adajcent=vec[sn[m]][i];
                    idSign[adajcent]++;
                    if(idSign[adajcent]==inNum){
                        MaxSign=0;
                        break;
                    }
                }
            }
            if(MaxSign==1){
                cout<<"Yes"<<endl;
            }
            else if(MaxSign==0){
                 cout<<"Not Maximal"<<endl;
            }
        }
        else{
           cout<<"Not a Clique"<<endl;
        }

    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章