A1142 Maximal Clique (25 point(s))

强连通子图

1. 原文

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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:

1
2
3
4
5
6
Yes
Yes
Yes
Yes
Not Maximal
Not a Clique

2. 解析

连通子图判断:图内边连通 graph[ a ] [ b ] != Inf

强连通子图:子图外的结点不存在与子图内结点都相连的点

set记录子图内结点值,bool visit[]记录访问的子图结点

循环遍历visit为false的结点i,判断该结点是否有与set中的点graph[ i ] [ *j ] != Inf,存在则不是强连通子图

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<cstdio>
#include<algorithm>
#include<set>
using namespace std;
const int maxn=210;
const int Inf=312312312;
int graph[maxn][maxn];
bool visit[maxn]={false};
set<int> s;
int main()
{
for (int i = 1; i < maxn; ++i)
{
for (int j = 1; j < maxn; ++j)
{
graph[i][j]=Inf;
}
graph[i][i]=1;
}
int nv,ne;
scanf("%d%d",&nv,&ne);
int a,b;
for (int i = 0; i < ne; ++i)
{
scanf("%d%d",&a,&b);
graph[a][b]=graph[b][a]=1;
}
int m;
scanf("%d",&m);
int k;
while(m--)
{
bool clique=true; bool maxiaml=true;
fill(visit,visit+maxn,false);
s.clear();
scanf("%d%d",&k,&a);
visit[a]=true;
s.insert(a);
for (int i = 1; i < k; ++i)
{
scanf("%d",&b);
s.insert(b);
if (graph[a][b]==Inf)
{
clique=false;
}
visit[b]=true;
a=b;
}
if (!clique)
{
printf("Not a Clique\n");
continue;
}
int cnt=0;
for (int i = 1; i <= nv; ++i)
{
if (visit[i]==false)
{
int flag=0;
for (set<int>::iterator j = s.begin(); j != s.end(); ++j)
{
if (graph[i][*j]==Inf)
{
flag=1;
break;
}
}
if (flag==0)
{
cnt++;
}
}
}
if (cnt==0)
{
printf("Yes\n");
}else{
printf("Not Maximal\n");
}

}
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/02/A1142/
  • 发布时间: 2019-09-02 16:11
  • 更新时间: 2021-10-29 14:10
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!