• PAT 1142 Maximal Clique(在几个数组中找到相同的点)


    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

    总结:唉,已经看懂了题目,怎么就是不能用代码好好表达我的意思呢!!!

    继续加油,前路漫漫亦灿灿,加油,在不久的将来会看到光明的!

    代码:(耗时长了很多,使用 hash 来存储当前数是否是题目给出的)

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. vectorint,bool>> v(210);
    6. int main(){
    7. int nv,ne,m,k;
    8. scanf("%d%d",&nv,&ne);
    9. for(int i=0;i
    10. int a,b;
    11. scanf("%d%d",&a,&b);
    12. v[a][b]=true;
    13. v[b][a]=true;
    14. }
    15. scanf("%d",&m);
    16. for(int i=0;i
    17. scanf("%d",&k);
    18. int a[k];
    19. map<int,bool> m;
    20. for(int i=0;i
    21. scanf("%d",&a[i]);
    22. m[a[i]]=true;
    23. }
    24. bool sign=true;
    25. for(int i=0;i
    26. bool flag=true;
    27. for(int j=i+1;j
    28. if(!v[a[i]][a[j]]){//确定不是Clique
    29. flag=false;
    30. break;
    31. }
    32. }
    33. if(!flag){
    34. sign=false;
    35. break;
    36. }
    37. }
    38. if(!sign) puts("Not a Clique");
    39. else{
    40. bool s=true;
    41. for(int i=1;i<=nv;i++){
    42. if(!m[i]){//如果这个点不是给出的点
    43. for(int j=0;j
    44. if(!v[i][a[j]]) break;//i表示当前循环到的点,j表示题目中给出的点
    45. if(j==k-1) s=false;
    46. }
    47. }
    48. if(!s){
    49. puts("Not Maximal");
    50. break;
    51. }
    52. }
    53. if(s) puts("Yes");
    54. }
    55. }
    56. return 0;
    57. }

     柳神代码:

    1. #include
    2. #include
    3. using namespace std;
    4. int e[210][210];
    5. int main() {
    6. int nv, ne, m, ta, tb, k;
    7. scanf("%d %d", &nv, &ne);
    8. for (int i = 0; i < ne; i++) {
    9. scanf("%d %d", &ta, &tb);
    10. e[ta][tb] = e[tb][ta] = 1;
    11. }
    12. scanf("%d", &m);
    13. for (int i = 0; i < m; i++) {
    14. scanf("%d", &k);
    15. vector<int> v(k);
    16. int hash[210] = {0}, isclique = 1, isMaximal = 1;
    17. for (int j = 0; j < k; j++) {
    18. scanf("%d", &v[j]);
    19. hash[v[j]] = 1;
    20. }
    21. for (int j = 0; j < k; j++) {
    22. if (isclique == 0) break;
    23. for (int l = j + 1; l < k; l++) {
    24. if (e[v[j]][v[l]] == 0) {
    25. isclique = 0;
    26. printf("Not a Clique\n");
    27. break;
    28. }
    29. }
    30. }
    31. if (isclique == 0) continue;
    32. for (int j = 1; j <= nv; j++) {//遍历所有的点
    33. if (hash[j] == 0) {//如果不是给出的点
    34. for (int l = 0; l < k; l++) {//遍历已给出的点,看是当前点在给出点中是否相邻
    35. if (e[v[l]][j] == 0) break;
    36. if (l == k - 1) isMaximal = 0;//如果说存在一个点是全部给出点的相邻点,那么说明不是Maximal
    37. }
    38. }
    39. if (isMaximal == 0) {
    40. printf("Not Maximal\n");
    41. break;
    42. }
    43. }
    44. if (isMaximal == 1) printf("Yes\n");
    45. }
    46. return 0;
    47. }

    好好学习,天天向上!

    我要考研

  • 相关阅读:
    联想笔记本win10无法启动vmware虚拟机
    CVE-2021-4034 polkit提权漏洞复现
    【C++】C++入门
    (附源码)ssmJavaEE无人机数据管理系统 毕业设计 111022
    ACL 访问控制 过滤数据 维护网络安全(第七课)
    化工行业分析:我国碳酸亚乙烯酯市场出口量为544.14吨,同比增长3.47%
    【uniapp】真机运行 访问电脑本地接口127.0.0.1网络失败(亲测有用!!)
    python篇-自动化-poco
    【力扣1844】将所有数字用字符替换
    Git快速入门一篇文章就够了。Git基础使用。如何将本地的文件上传到Gitee?
  • 原文地址:https://blog.csdn.net/weixin_50679551/article/details/127619833