题目链接

题意 : 给出两幅顶点数一样的图 G1、G2 ,现在要求在 G2 中选出一些边集、使之构成一幅新的图 G ,要求 G 要与 G1 同构,现在要你统计合法的 G 有多少种

分析 : 

图的同构是离散数学里面图论的一个概念、具体的可以看 这里

判断两幅图是否是同构的至今貌似还是 NP 问题

由于顶点数最多只有 8、同时意味着边最多只有 28

那么可以由此想到 O(n!) 枚举所有的顶点的双射 (实际就是枚举全排列)

考察每个双射下两图是否能够构成同构关系

判断是否构成双射只要考察其邻接矩阵是否能一样即可

这就意味着去选出 G2 这副图中的某些边集

使得当前枚举到的双射能够使得 G1 和 G2 同构

由于边不多、所以可以状态压缩这些边集

存到一个 set 中进行去重、最后 set 的大小即为答案

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

还有另外一种做法就是

枚举双射计算出 G1 和 G2 同构方案数

然后再计算出 G1 和 G1 自己的自同构方案数

最后答案就是 ( G1 和 G2 同构方案数 ) / ( G1 和 G1 自己的自同构方案数 )

说实话、这个东西、我并不是很理解...........

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define ULL unsigned long long
  4.  
  5. #define scl(i) scanf("%lld", &i)
  6. #define scll(i, j) scanf("%lld %lld", &i, &j)
  7. #define sclll(i, j, k) scanf("%lld %lld %lld", &i, &j, &k)
  8. #define scllll(i, j, k, l) scanf("%lld %lld %lld %lld", &i, &j, &k, &l)
  9.  
  10. #define scs(i) scanf("%s", i)
  11. #define sci(i) scanf("%d", &i)
  12. #define scd(i) scanf("%lf", &i)
  13. #define scIl(i) scanf("%I64d", &i)
  14. #define scii(i, j) scanf("%d %d", &i, &j)
  15. #define scdd(i, j) scanf("%lf %lf", &i, &j)
  16. #define scIll(i, j) scanf("%I64d %I64d", &i, &j)
  17. #define sciii(i, j, k) scanf("%d %d %d", &i, &j, &k)
  18. #define scddd(i, j, k) scanf("%lf %lf %lf", &i, &j, &k)
  19. #define scIlll(i, j, k) scanf("%I64d %I64d %I64d", &i, &j, &k)
  20. #define sciiii(i, j, k, l) scanf("%d %d %d %d", &i, &j, &k, &l)
  21. #define scdddd(i, j, k, l) scanf("%lf %lf %lf %lf", &i, &j, &k, &l)
  22. #define scIllll(i, j, k, l) scanf("%I64d %I64d %I64d %I64d", &i, &j, &k, &l)
  23.  
  24. #define lson l, m, rt<<1
  25. #define rson m+1, r, rt<<1|1
  26. #define lowbit(i) (i & (-i))
  27. #define mem(i, j) memset(i, j, sizeof(i))
  28.  
  29. #define fir first
  30. #define sec second
  31. #define VI vector<int>
  32. #define ins(i) insert(i)
  33. #define pb(i) push_back(i)
  34. #define pii pair<int, int>
  35. #define mk(i, j) make_pair(i, j)
  36. #define all(i) i.begin(), i.end()
  37. #define pll pair<long long, long long>
  38.  
  39. #define _TIME 0
  40. #define _INPUT 0
  41. #define _OUTPUT 0
  42. clock_t START, END;
  43. void __stTIME();
  44. void __enTIME();
  45. void __IOPUT();
  46. using namespace std;
  47.  
  48. + ;
  49.  
  50. int G1[maxn][maxn];
  51. int G2[maxn][maxn];
  52.  
  53. map<pii, int> mp;
  54. set<LL> ans;
  55.  
  56. int main(void){__stTIME();__IOPUT();
  57.  
  58. int n, m1, m2;
  59.  
  60. while(~sciii(n, m1, m2)){
  61.  
  62. mem(G1, );
  63. mem(G2, );
  64. ans.clear();
  65. mp.clear();
  66.  
  67. ; i<m1; i++){
  68. int u, v;
  69. scii(u, v);
  70. G1[u][v] = G1[v][u] = ;
  71. }
  72.  
  73. ; i<m2; i++){
  74. int u, v;
  75. scii(u, v);
  76. if(u < v) swap(u, v);
  77. mp[mk(u,v)] = i;
  78. G2[u][v] = G2[v][u] = ;
  79. }
  80.  
  81. int idx[maxn];
  82. ; i<=n; i++) idx[i] = i;
  83.  
  84. do{
  85. LL state = ;
  86. bool ok = true;
  87. ; i<=n; i++){///判断是否能构成同构
  88. ; j<=n; j++){
  89. if(G1[i][j] && !G2[idx[i]][idx[j]]){///只考虑G1有边关联的两个顶点的情况
  90. ok = false;
  91. break;
  92. }else{
  93. if(G1[i][j]){
  94. int u = idx[i];
  95. int v = idx[j];
  96. if(u < v) swap(u, v);
  97. state |= (1LL<<mp[mk(u,v)]);///状态压缩
  98. }
  99. }
  100. }
  101. if(!ok) break;
  102. }
  103.  
  104. if(!ok) continue;
  105.  
  106. ans.ins(state);
  107.  
  108. }, idx++n));
  109.  
  110. printf("%d\n", (int)ans.size());
  111. }
  112.  
  113. __enTIME();;}
  114.  
  115. void __stTIME()
  116. {
  117. #if _TIME
  118. START = clock();
  119. #endif
  120. }
  121.  
  122. void __enTIME()
  123. {
  124. #if _TIME
  125. END = clock();
  126. cerr<<"execute time = "<<(double)(END-START)/CLOCKS_PER_SEC<<endl;
  127. #endif
  128. }
  129.  
  130. void __IOPUT()
  131. {
  132. #if _INPUT
  133. freopen("in.txt", "r", stdin);
  134. #endif
  135. #if _OUTPUT
  136. freopen("out.txt", "w", stdout);
  137. #endif
  138. }

Nowcoder Two Graphs ( 图的同构 )的更多相关文章

  1. USTC 1119 graph 图的同构

    USTC 1119 图的同构的严格定义可以参考离散数学:The simple graphs G1=(V1,E1) and G2=(V2,E2)are isomorphic if there exist ...

  2. 2018牛客网暑期ACM多校训练营(第一场) D - Two Graphs - [无向图同构]

    题目链接:https://www.nowcoder.com/acm/contest/139/D 题目描述 Two undirected simple graphs  and  where  are i ...

  3. Two Graphs 牛客网暑期ACM多校训练营(第一场)D 图论基础知识 全排列

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 Two undirected simple graphs and where are isomo ...

  4. tunning-Instruments and Flame Graphs

    On mac os, programs may need Instruments to tuning, and when you face too many probe messages, you'l ...

  5. Intel® Threading Building Blocks (Intel® TBB) Developer Guide 中文 Parallelizing Data Flow and Dependence Graphs并行化data flow和依赖图

    https://www.threadingbuildingblocks.org/docs/help/index.htm Parallelizing Data Flow and Dependency G ...

  6. 特征向量-Eigenvalues_and_eigenvectors#Graphs

    https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors#Graphs A               {\displaystyle A} ...

  7. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  8. NowCoder猜想(素数筛法+位压缩)

    在期末被各科的大作业碾压快要窒息之际,百忙之中抽空上牛客网逛了逛,无意中发现一道好题,NowCoder猜想,题意很明显,就是个简单的素数筛法,但竟然超内存了,我晕(+﹏+)~  明明有 3 万多 k ...

  9. [nowCoder] 两个不等长数组求第K大数

    给定两个有序数组arr1和arr2,在给定一个整数k,返回两个数组的所有数中第K小的数.例如:arr1 = {1,2,3,4,5};arr2 = {3,4,5};K = 1;因为1为所有数中最小的,所 ...

随机推荐

  1. 自然语言处理工具pyhanlp分词与词性标注

    Pyhanlp分词与词性标注的相关内容记得此前是有分享过的.可能时间太久记不太清楚了.以下文章是分享自“baiziyu”所写(小部分内容有修改),供大家学习参考之用. 简介 pyhanlp是HanLP ...

  2. python 爬虫--下载图片,下载音乐

    #下载图片 imgUrl='http://www.pptbz.com/pptpic/UploadFiles_6909/201211/2012111719294197.jpg' r=requests.g ...

  3. Maven添加镜像仓库、更改本地仓库位置

    添加镜像仓库 在conf目录下的settings.xml文件的145行左右 id表示该镜像的id mirrorOf表示为哪个仓库配置镜像,central为默认的中央仓库的id,也可以使用通配符*,来匹 ...

  4. P2670 【扫雷游戏】

    题面哦~~ lalala~~~ 这题数据并不大,最大不过100*100,所以果断穷举 其实本来我是想边读边做的,但读入是个问题. 主要思路呢,就是读完之后穷举一边,只要是炸弹,就把周围一圈8个加一遍 ...

  5. myeclipse使用db-brower连接到sqlserver2012踩坑经历

    myeclipse使用db-brower连接到sqlserver踩坑经历 首先得建立个角色 右键->创建登录名 权限开大点 连接设置 Driver template选择我选这个,格式按照我的写 ...

  6. C++ 多态、虚函数(virtual 关键字)、静态联编、动态联编

    函数重写:(在子类中重写父类中的函数) 父类中被重写的函数  依然会继承  给子类. 子类中重写的函数将覆盖父类中的函数. 通过作用域分辨符  ::  可以访问到父类中的函数. 例如: #includ ...

  7. vm文件

    <html> <head> <title>编队管理</title> </head> <style type="text/cs ...

  8. private修饰的方法可以通过反射访问,那么private的意义是什么?

    反射代码: package test; public class Person { private String userName= "Tom"; private void pla ...

  9. CentOS7部署HDP3.1.0.0

    Apache Ambari是一个基于Web的支持Apache Hadoop集群的供应.管理和监控的开源工具,Ambari已支持大多数Hadoop组件,包括HDFS.MapReduce.Hive.Pig ...

  10. C++ Concurrency In Action 一些重点

    全部来自于gitbook  C++并发编程(中文版) 需要对一个还未销毁的std::thread对象使用join()或detach().如果想要分离一个线程,可以在线程启动后,直接使用detach() ...