• 1142 Maximal Clique 甲级xp_xht123


    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. clique : 在集合中,任意两个不同的点,在图中都有边相连
    2. maximal clique : 是clique , 如果该clique不能通过加入某个新的顶点来扩展成一个更大的Clique

    根据这两条性质,很容易的判断是哪种情况,首先先看联通性,其次再看除了集合中的点是否还跟集合中的点有边。

    1. /*
    2. clique : 在集合中,任意两个不同的点,在图中都有边相连
    3. maximal clique : 是clique , 如果该clique不能通过加入某个新的顶点来扩展成一个更大的Clique
    4. */
    5. #include
    6. #include
    7. #include
    8. #include
    9. using namespace std;
    10. const int N = 210;
    11. int n , m;
    12. bool g[N][N] , st[N];
    13. vector<int>v;
    14. bool check1()
    15. {
    16. for(int i = 0;i < v.size();i ++)
    17. for(int j = 0;j < i;j ++)
    18. if(!g[v[i]][v[j]]) return false;
    19. return true;
    20. }
    21. bool check2()
    22. {
    23. memset(st , false , sizeof st);
    24. for(int i = 0;i < v.size();i ++) st[v[i]] = true;//标记所有clique中的点
    25. for(int i = 1;i <= n;i ++)
    26. {
    27. if(!st[i]) //不是clique的点
    28. {
    29. bool flag = true;
    30. for(int j = 0;j < v.size();j ++)
    31. {
    32. if(!g[i][v[j]])
    33. {
    34. flag = false;
    35. break;
    36. }
    37. }
    38. if(flag) return false;
    39. }
    40. }
    41. return true;
    42. }
    43. int main()
    44. {
    45. cin >> n >> m;
    46. while(m --)
    47. {
    48. int a , b;
    49. cin >> a >> b;
    50. g[a][b] = g[b][a] = true;
    51. }
    52. int k;
    53. cin >> k;
    54. while(k --)
    55. {
    56. int x;
    57. cin >> x;
    58. for(int i = 0;i < x;i ++)
    59. {
    60. int num;
    61. cin >> num;
    62. v.push_back(num);
    63. }
    64. //判断是否是clique
    65. if(check1())
    66. {
    67. if(check2()) puts("Yes");
    68. else puts("Not Maximal");
    69. }
    70. else puts("Not a Clique");
    71. v.clear();
    72. }
    73. return 0;
    74. }

  • 相关阅读:
    【华为OD机试真题 JAVA】叠积木
    系统韧性研究(2)|系统韧性如何关联其他质量属性?
    mock的基本用法
    百度网盘svip白嫖永久手机2024最新教程
    GIS前端-地图事件编程
    ORB-SLAM2 ---- Tracking::CreateInitialMapMonocular函数
    2022-08-06
    知识图谱丨知识图谱赋能企业数字化转型
    使用vue-cli来搭建vue项目
    libnice 源码分析
  • 原文地址:https://blog.csdn.net/xp_xht123/article/details/126298137