• A1142 Maximal Clique(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【图+极大团】


    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. 是否两两相连
    2. 是否存在每个人都有的共同邻居

    然后分别定义两个判断上述条件的bool类型函数,再根据要求在输出时进行判断 

    代码如下:

    1. #include
    2. using namespace std;
    3. const int maxn = 210;
    4. int Nv, Ne, v1, v2, m;
    5. int edge[maxn][maxn] = { 0 };//邻接矩阵
    6. bool isconnected(vector<int> vlist) {
    7. //判断是否两两相连
    8. bool flag = true;
    9. for (int i = 0; i < vlist.size()-1; i++) {
    10. for (int j = i + 1; j < vlist.size(); j++) {
    11. if (edge[vlist[i]][vlist[j]] != 1) {
    12. flag = false;
    13. return flag;
    14. }
    15. }
    16. }
    17. return flag;
    18. }
    19. bool addNeighbour(vector<int> vlist) {
    20. bool flag = false;//true可以加邻居
    21. for (int i = 1; i <= Nv; i++) {
    22. if (find(vlist.begin(), vlist.end(), i) == vlist.end()) {//该节点不在查询队列里
    23. for (int j = 0; j < vlist.size(); j++) {
    24. if (edge[vlist[j]][i] != 1) {
    25. flag = false;
    26. break;
    27. }
    28. else flag = true;
    29. }
    30. }
    31. if (flag)break;
    32. }
    33. return flag;
    34. }
    35. int main()
    36. {
    37. cin >> Nv >> Ne;
    38. //输入
    39. for (int i = 0; i < Ne; i++) {
    40. cin >> v1 >> v2;
    41. edge[v1][v2] = edge[v2][v1] = 1;
    42. }
    43. //查询,判断是否是极大团
    44. //1、是否两两相连
    45. //2、是否存在每个人都有的共同邻居
    46. cin >> m;
    47. int num,v;
    48. for (int i = 0; i < m; i++) {
    49. cin >> num;
    50. vector<int> querylist(num);
    51. for (int j = 0; j < num; j++) {
    52. cin >> querylist[j];
    53. }
    54. sort(querylist.begin(), querylist.end());
    55. if (isconnected(querylist)) {
    56. if(!addNeighbour(querylist))cout << "Yes" << endl;
    57. else cout << "Not Maximal" << endl;;
    58. }
    59. else cout << "Not a Clique" << endl;
    60. }
    61. return 0;
    62. }

    运行结果如下:

     

  • 相关阅读:
    本地快速部署 TiDB 集群
    ATL新能源科技薪资待遇及Verify测评语言理解数字推理题型简介
    Tailscale的子网路由和出口节点
    05-Zookeeper典型使用场景实战
    向上转型 向下转型 重写 多态 ---java
    Java中的命名规则
    人工智能顶级会议 和 热门方向 记录
    查看和修改自己的git提交时的作者信息
    JavaScript常用知识点
    Spring
  • 原文地址:https://blog.csdn.net/qq_47677800/article/details/126229442