PAT_A1142#Maximal Clique

Source:

PAT A1142 Maximal Clique (25 分)

Description:

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

Keys:

Attention:

  • 想的有点多,数据规模不大,暴力求解就好了

Code:

 1 /*
 2 Data: 2019-08-06 20:50:04
 3 Problem: PAT_A1142#Maximal Clique
 4 AC: 30:25
 5 
 6 题目大意:
 7 判断所给子图是否是完全图(无向图)
 8 输入:
 9 第一行给出,顶点数Nv<=200,Ne边数
10 接下来Ne行,v1,v2
11 接下来一行,给出查询数M;
12 接下来M行,给出顶点数K,和K个顶点;
13 输出:
14 YES,最大完全子图;
15 Not Maximal,完全子图,非最大
16 Not a Clique,完全图
17 
18 基本思路:
19 先判断所给子图是否是完全图,
20 再遍历图中其余顶点,加入该子图,判断是否为完全图
21 */
22 #include<cstdio>
23 #include<algorithm>
24 using namespace std;
25 const int M=1e3,INF=1e9;
26 int grap[M][M],v[M],vis[M];
27 
28 int main()
29 {
30 #ifdef ONLINE_JUDGE
31 #else
32     freopen("Test.txt", "r", stdin);
33 #endif // ONLINE_JUDGE
34 
35     int n,m,k,v1,v2;
36     scanf("%d%d", &n,&m);
37     fill(grap[0],grap[0]+M*M,INF);
38     for(int i=0; i<m; i++)
39     {
40         scanf("%d%d", &v1,&v2);
41         grap[v1][v2]=1;
42         grap[v2][v1]=1;
43     }
44     scanf("%d", &m);
45     while(m--)
46     {
47         scanf("%d", &k);
48         fill(vis,vis+n+1,0);
49         for(int i=0; i<k; i++)
50         {
51             scanf("%d", &v[i]);
52             vis[v[i]]=1;
53         }
54         for(int i=0; i<k-1; i++)
55         {
56             v1 = v[i];
57             for(int j=i+1; j<k; j++)
58             {
59                 v2=v[j];
60                 if(grap[v1][v2]==INF)
61                 {
62                     k=0;
63                     break;
64                 }
65                 v1=v2;
66             }
67         }
68         if(k==0)
69         {
70             printf("Not a Clique\n");
71             continue;
72         }
73         for(int i=1; i<=n; i++)
74         {
75             if(vis[i]==0)
76             {
77                 for(int j=0; j<k; j++)
78                 {
79                     if(grap[i][v[j]]==INF)
80                     {
81                         vis[i]=1;
82                         break;
83                     }
84                 }
85                 if(vis[i]==0)
86                 {
87                     printf("Not Maximal\n");
88                     k=0;break;
89                 }
90             }
91         }
92         if(k)
93             printf("Yes\n");
94     }
95 
96     return 0;
97 }

 

posted @ 2019-05-23 23:01  林東雨  阅读(136)  评论(0编辑  收藏  举报