1. 程式人生 > >PAT 1142 Maximal Clique[難]

PAT 1142 Maximal Clique[難]

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是一個點集,在一個無向圖中,這個點集中任意兩個不同的點之間都是相連的。maximal clique是一個clique,這個clique不可以再加入任何一個新的結點構成新的clique。

輸入是有n條邊,給出每條邊的兩端節點,並且後面給出m個查詢,查詢是點集。

 //這個題目大意看了好幾遍沒看懂,這個clique也不是環,比如對第三個查詢2 3,輸出Yes,說明不是環了。

//思考了一下發現不太會,怎麼去確定這個是maximal的呢?怎麼去擴充套件判斷呢?不會。

程式碼轉自:https://www.liuchuo.net/archives/4614

#include <iostream>
#include <vector>
#include<cstdio>
using namespace std;
int e[210][210];
int main() {
    int nv, ne, m, ta, tb, k;
    scanf("%d %d", &nv, &ne);
    for (int i = 0; i < ne; i++) {
        scanf("%d %d", &ta, &tb);
        e[ta][tb] = e[tb][ta] = 1;//儲存到鄰接矩陣中,有邊是1.
    }
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        scanf("%d", &k);
        vector<int> v(k);
        int hash[210] = {0}, isclique = 1, isMaximal = 1;
        for (int j = 0; j < k; j++) {
            scanf("%d", &v[j]);
            hash[v[j]] = 1;//使用hash陣列儲存
        }
        for (int j = 0; j < k; j++) {//這裡判斷是否是一個click
            if (isclique == 0) break;//跳出兩層迴圈
            for (int l = j + 1; l < k; l++) {
                if (e[v[j]][v[l]] == 0) {
                    isclique = 0;
                    printf("Not a Clique\n");
                    break;
                }
            }
        }
        if (isclique == 0) continue;//不進行下面的操作。
        for (int j = 1; j <= nv; j++) {
            if (hash[j] == 0) {//挨個判斷其他所有的點,判斷每一個點。
                for (int l = 0; l < k; l++) {//和當前檢測中所有的點進行判斷。
                    if (e[v[l]][j] == 0) break;//如果這個點不是的話,接著判斷其他點
                    if (l == k - 1) isMaximal = 0;
                }
            }
            if (isMaximal == 0) {
                printf("Not Maximal\n");
                break;
            }
        }
        if (isMaximal == 1) printf("Yes\n");
    }
    return 0;
}

 

//柳神真厲害。

1.使用鄰接矩陣儲存圖,右邊標記為1.

2.對於輸入的使用hash陣列來標記,向量來儲存

3.對圖中所有剩下的點一一與當前檢測中的進行判斷。

 //學習了!