Nowcoder Two Graphs ( 图的同构 )
题意 : 给出两幅顶点数一样的图 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 自己的自同构方案数 )
说实话、这个东西、我并不是很理解...........
#include<bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define scl(i) scanf("%lld", &i)
#define scll(i, j) scanf("%lld %lld", &i, &j)
#define sclll(i, j, k) scanf("%lld %lld %lld", &i, &j, &k)
#define scllll(i, j, k, l) scanf("%lld %lld %lld %lld", &i, &j, &k, &l)
#define scs(i) scanf("%s", i)
#define sci(i) scanf("%d", &i)
#define scd(i) scanf("%lf", &i)
#define scIl(i) scanf("%I64d", &i)
#define scii(i, j) scanf("%d %d", &i, &j)
#define scdd(i, j) scanf("%lf %lf", &i, &j)
#define scIll(i, j) scanf("%I64d %I64d", &i, &j)
#define sciii(i, j, k) scanf("%d %d %d", &i, &j, &k)
#define scddd(i, j, k) scanf("%lf %lf %lf", &i, &j, &k)
#define scIlll(i, j, k) scanf("%I64d %I64d %I64d", &i, &j, &k)
#define sciiii(i, j, k, l) scanf("%d %d %d %d", &i, &j, &k, &l)
#define scdddd(i, j, k, l) scanf("%lf %lf %lf %lf", &i, &j, &k, &l)
#define scIllll(i, j, k, l) scanf("%I64d %I64d %I64d %I64d", &i, &j, &k, &l)
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define lowbit(i) (i & (-i))
#define mem(i, j) memset(i, j, sizeof(i))
#define fir first
#define sec second
#define VI vector<int>
#define ins(i) insert(i)
#define pb(i) push_back(i)
#define pii pair<int, int>
#define mk(i, j) make_pair(i, j)
#define all(i) i.begin(), i.end()
#define pll pair<long long, long long>
#define _TIME 0
#define _INPUT 0
#define _OUTPUT 0
clock_t START, END;
void __stTIME();
void __enTIME();
void __IOPUT();
using namespace std;
+ ;
int G1[maxn][maxn];
int G2[maxn][maxn];
map<pii, int> mp;
set<LL> ans;
int main(void){__stTIME();__IOPUT();
int n, m1, m2;
while(~sciii(n, m1, m2)){
mem(G1, );
mem(G2, );
ans.clear();
mp.clear();
; i<m1; i++){
int u, v;
scii(u, v);
G1[u][v] = G1[v][u] = ;
}
; i<m2; i++){
int u, v;
scii(u, v);
if(u < v) swap(u, v);
mp[mk(u,v)] = i;
G2[u][v] = G2[v][u] = ;
}
int idx[maxn];
; i<=n; i++) idx[i] = i;
do{
LL state = ;
bool ok = true;
; i<=n; i++){///判断是否能构成同构
; j<=n; j++){
if(G1[i][j] && !G2[idx[i]][idx[j]]){///只考虑G1有边关联的两个顶点的情况
ok = false;
break;
}else{
if(G1[i][j]){
int u = idx[i];
int v = idx[j];
if(u < v) swap(u, v);
state |= (1LL<<mp[mk(u,v)]);///状态压缩
}
}
}
if(!ok) break;
}
if(!ok) continue;
ans.ins(state);
}, idx++n));
printf("%d\n", (int)ans.size());
}
__enTIME();;}
void __stTIME()
{
#if _TIME
START = clock();
#endif
}
void __enTIME()
{
#if _TIME
END = clock();
cerr<<"execute time = "<<(double)(END-START)/CLOCKS_PER_SEC<<endl;
#endif
}
void __IOPUT()
{
#if _INPUT
freopen("in.txt", "r", stdin);
#endif
#if _OUTPUT
freopen("out.txt", "w", stdout);
#endif
}
Nowcoder Two Graphs ( 图的同构 )的更多相关文章
- USTC 1119 graph 图的同构
USTC 1119 图的同构的严格定义可以参考离散数学:The simple graphs G1=(V1,E1) and G2=(V2,E2)are isomorphic if there exist ...
- 2018牛客网暑期ACM多校训练营(第一场) D - Two Graphs - [无向图同构]
题目链接:https://www.nowcoder.com/acm/contest/139/D 题目描述 Two undirected simple graphs and where are i ...
- Two Graphs 牛客网暑期ACM多校训练营(第一场)D 图论基础知识 全排列
链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 Two undirected simple graphs and where are isomo ...
- tunning-Instruments and Flame Graphs
On mac os, programs may need Instruments to tuning, and when you face too many probe messages, you'l ...
- 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 ...
- 特征向量-Eigenvalues_and_eigenvectors#Graphs
https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors#Graphs A {\displaystyle A} ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- NowCoder猜想(素数筛法+位压缩)
在期末被各科的大作业碾压快要窒息之际,百忙之中抽空上牛客网逛了逛,无意中发现一道好题,NowCoder猜想,题意很明显,就是个简单的素数筛法,但竟然超内存了,我晕(+﹏+)~ 明明有 3 万多 k ...
- [nowCoder] 两个不等长数组求第K大数
给定两个有序数组arr1和arr2,在给定一个整数k,返回两个数组的所有数中第K小的数.例如:arr1 = {1,2,3,4,5};arr2 = {3,4,5};K = 1;因为1为所有数中最小的,所 ...
随机推荐
- Java 虚拟机的运行模式
这几天在读周志明老师的<深入理解JVM虚拟机> 讲到了 java的运行模式, 有mixed 模式 interpret模式还有compile模式.效果如下面所示 java -version ...
- 自己总结的keepalived的配置流程以及注意事项
编写背景:上班时领导要求我们团队实现postgresql主备切换的高可用问题,我辅助keepalived的部分,从查资料到实施最后使用,最后编写了这个博客,水平有限,欢迎大家指正 ###postgre ...
- JS中的继承(下)
JS中的继承(下) 在上一篇 JS中的继承(上) 我们介绍了3种比较常用的js继承方法,如果你没看过,那么建议你先看一下,因为接下来要写的内容, 是建立在此基础上的.另外本文作为我个人的读书笔记,才疏 ...
- SQL----Scalar 函数
UCASE() 函数 UCASE 函数把字段的值转换为大写. SQL UCASE() 语法 SELECT UCASE(column_name) FROM table_name SQL UCASE() ...
- POJ题解Sorting It All Out-传递丢包+倍增
题目链接: http://poj.org/problem?id=1094 题目大意(直接从谷歌翻译上复制下来的): 描述 不同值的递增排序顺序是其中使用某种形式的小于运算符来将元素从最小到最大排序的顺 ...
- Flask与微信小程序登录(后端)
开发微信小程序时,接入小程序的授权登录可以快速实现用户注册登录的步骤,是快速建立用户体系的重要一步.这篇文章将介绍 python + flask + 微信小程序实现用户快速注册登录方案(本文主要进行后 ...
- kinit: Bad encryption type while getting initial credentials
描述:RHEL 6.x主机执行kinit -kt命令报如下错误 [heboan@localhost~]$ kinit -kt heboan.keytab heboan kinit: Bad encry ...
- 会计凭证替代 OBBH
单词:Validation: 会计凭证校验 ,Substitution:会计凭证替代 step1: GCX2 gblr: ZRFI_GGBR000 gbls: ZRFI_GGBS000 - step2 ...
- Python(os和sys)理解
Python(os和sys)理解 os模块负责程序与操作系统的交互,提供了访问操作系统底层的接口; sys模块负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时 ...
- jq选择CheckBox进行排序
<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name=& ...