• 1142 Maximal Clique


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

    Sample Output:

    1. Yes
    2. Yes
    3. Yes
    4. Yes
    5. Not Maximal
    6. Not a Clique
    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int ne, nv, m, k, u, v;
    6. int a[300];
    7. bool g[300][300];
    8. bool vis[300];
    9. int main() {
    10. cin >> nv >> ne;
    11. for (int i = 1; i <= nv; i++) {
    12. g[i][i] = 1;
    13. }
    14. for (int i = 0; i < ne; i++) {
    15. cin >> u >> v;
    16. g[u][v] = g[v][u] = 1;
    17. }
    18. cin >> m;
    19. while (m--) {
    20. cin >> k;
    21. int flag = 0;
    22. memset(vis, 0, sizeof(vis));
    23. for (int i = 0; i < k; i++) {
    24. cin >> a[i];
    25. vis[a[i]] = 1;
    26. }
    27. for (int i = 0; i < k; i++) {
    28. if (flag == 1) {
    29. break;
    30. }
    31. for (int j = i + 1; j < k; j++) {
    32. if (!g[a[i]][a[j]]) {
    33. flag = 1;
    34. break;
    35. }
    36. }
    37. }
    38. if (flag == 1) {
    39. cout << "Not a Clique" << endl;
    40. continue;
    41. }
    42. if (flag == 0) {
    43. for (int i = 1; i <= nv; i++) {
    44. if (flag == 2) {
    45. break;
    46. }
    47. if (!vis[i]) {
    48. vis[i] = 1;
    49. int j = 0;
    50. while (j < k && g[i][a[j]]) {
    51. j++;
    52. }
    53. if (j == k) {
    54. flag = 2;
    55. break;
    56. }
    57. }
    58. }
    59. }
    60. if (flag == 2) {
    61. cout << "Not Maximal" << endl;
    62. } else {
    63. cout << "Yes" << endl;
    64. }
    65. }
    66. return 0;
    67. }
  • 相关阅读:
    通过Oracle Enterprise Manager管理单实例数据库
    vue3黑马笔记
    Unity转换字符串中文繁简体
    linux下C++开发环境搭建
    基础sed命令
    【微服务开篇-RestTemplate服务调用、Eureka注册中心、Nacos注册中心】
    ARM DIY(九)陀螺仪调试
    SpringBoot SpringBoot 基础篇 4 基于 SpringBoot 的SSMP 整合案例 4.2 SSMP 整合案例模块创建
    React Native Android设备连接到ADB后 yarn start操作后找不到设备
    20T算力打造轻地图方案,这家智驾公司持续内卷
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/128068793