输入样例:
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
输出样例:
Yes
Yes
Yes
Yes
Not Maximal
Not a Clique
解题思路:
根据题意模拟:给定点集中有不相连的边,则不是团;是团时,点集外的点存在有某点与团内所有点都有边,则不是最大团,否则是最大团。时间复杂度为 O(n·n·m),最坏为 4·10^6
Java代码:
import java.io.*;
import java.util.Arrays;
public class Main {
static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public static int ini() throws IOException {
st.nextToken();
return (int)st.nval;
}
public static void main(String[] args) throws IOException {
int n = ini(), m = ini();
boolean [][]g = new boolean[n + 1][n + 1];
while(m-- > 0) {
int a = ini(), b = ini();
g[a][b] = g[b][a] = true;
}
int q = ini();
boolean []vis = new boolean[205];
while(q-- > 0) {
int k = ini();
int []arr = new int[k]; // 保存查询的点
Arrays.fill(vis, false);
for(int i = 0; i < k; i++) {
arr[i] = ini();
vis[arr[i]] = true; // 标记已经在团中的点
}
boolean flag = true;
for(int i = 0; i < k; i++) {
for(int j = 0; j < k; j++)
if(!g[arr[i]][arr[j]]) flag = false; // 存在没有边相连的两点
if(!flag) break;
}
if(!flag) {
System.out.println("Not a Clique");
continue;
}
flag = true;
for(int i = 1; i <= n; i++) {
if(vis[i]) continue; // 只检查在团外的点
int j = 0;
for(j = 0; j < k; j++)
if(!g[i][arr[j]]) break; // 团外的某点与团内的某点没有边,肯定该点不能加入
if(j == k) { // 都有边,则不是最大团
System.out.println("Not Maximal");
flag = false;
break;
}
}
if(flag) System.out.println("Yes");
}
}
}