题目链接:https://www.nowcoder.com/acm/contest/139/D

题目描述

Two undirected simple graphs  and  where  are isomorphic when there exists a bijection  on V satisfying  if and only if {x, y} ∈ E2.
Given two graphs  and , count the number of graphs  satisfying the following condition: 
.
* G1 and G are isomorphic.

输入描述:

The input consists of several test cases and is terminated by end-of-file.
The first line of each test case contains three integers n, m1 and m2 where |E1| = m1 and |E2| = m2.
The i-th of the following m1 lines contains 2 integers ai and bi which denote {ai, bi} ∈ E1.
The i-th of the last m2 lines contains 2 integers ai and bi which denote {ai, bi} ∈ E2.

输出描述:

For each test case, print an integer which denotes the result.

输入

3 1 2
1 3
1 2
2 3
4 2 3
1 2
1 3
4 1
4 2
4 3

输出

2
3

备注:

* 1 ≤ n ≤ 8
*

* 1 ≤ ai,bi ≤ n
* The number of test cases does not exceed 50.

题意:

两个简单无向图G1和G2,问G2的子图中有多少个与G1同构。

题解:

显然枚举子图不现实,假设phi(x)是从G1中的某个点映射到G2的某个点的函数,

那么,从最开始 phi[1:n] = (1,2,3,…,n) 开始全排列,可以枚举出G1对应到G2的全部情况,只要判断G2中有没有相应的边即可。

同时,要考虑自同构的情况,通过G1映射到自身判断是否自同构,最后答案除以自同构数即可。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int maxm=maxn*(maxn-)/; int n,m1,m2;
int E1[maxn][maxn],u[maxm],v[maxm];
int E2[maxn][maxn];
int phi[maxn]; int main()
{
while(scanf("%d%d%d",&n,&m1,&m2)!=EOF)
{
memset(E1,,sizeof(E1));
memset(E2,,sizeof(E2));
for(int i=;i<=m1;i++) //G1
{
scanf("%d%d",&u[i],&v[i]);
E1[u[i]][v[i]]=E1[v[i]][u[i]]=;
}
for(int i=,a,b;i<=m2;i++) //G2
{
scanf("%d%d",&a,&b);
E2[a][b]=E2[b][a]=;
} for(int i=;i<=n;i++) phi[i]=i; //初始化映射函数
int ans=,cnt=;
do
{
bool ok=; //标记是否与G2的某个子图同构
bool self=; //标记是否自同构
for(int i=;i<=m1;i++)
{
int a=phi[u[i]],b=phi[v[i]];
if(E2[a][b]==) ok=;
if(E1[a][b]==) self=;
}
if(ok) ans++;
if(self) cnt++;
}while(next_permutation(phi+,phi+n+)); printf("%d\n",ans/cnt);
}
}

2018牛客网暑期ACM多校训练营(第一场) D - Two Graphs - [无向图同构]的更多相关文章

  1. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  2. 2018牛客网暑期ACM多校训练营(第一场)D图同构,J

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...

  3. 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)

    Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...

  4. 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)

    链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...

  5. 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)

    链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...

  6. 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)

    题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...

  7. 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)

    题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...

  8. 2018牛客网暑期ACM多校训练营(第三场) A - PACM Team - [四维01背包][四约束01背包]

    题目链接:https://www.nowcoder.com/acm/contest/141/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  9. 2018牛客网暑期ACM多校训练营(第五场) F - take - [数学期望][树状数组]

    题目链接:https://www.nowcoder.com/acm/contest/143/F 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  10. 2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]

    题目链接:https://www.nowcoder.com/acm/contest/143/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

随机推荐

  1. [OpenCV] Samples 06: logistic regression

    logistic regression,这个算法只能解决简单的线性二分类,在众多的机器学习分类算法中并不出众,但它能被改进为多分类,并换了另外一个名字softmax, 这可是深度学习中响当当的分类算法 ...

  2. 数据注解特性--NotMapped

    NotMapped特性可以应用到领域类的属性中,Code-First默认的约定,是为所有带有get,和set属性选择器的属性创建数据列.. NotManpped特性打破了这个约定,你可以使用NotMa ...

  3. javascript使用jQuery加载CSV文件+ajax关闭异步

    <script src="jquery-3.3.1.min.js"></script>定义一个csv函数// 关闭异步,否则cesium初始化的时候,csv ...

  4. 第二篇:Hadoop 在Ubuntu Kylin系统上的搭建[图解]

    前言 本文介绍如何在Ubuntu Kylin操作系统上搭建Hadoop平台. 配置 1. 操作系统: Ubuntu Kylin 14.04 2. 编程语言: JDK 1.8 3. 通信协议: SSH ...

  5. hadoop核心逻辑shuffle代码分析-map端

    首先要推荐一下:http://www.alidata.org/archives/1470 阿里的大牛在上面的文章中比较详细的介绍了shuffle过程中mapper和reduce的每个过程,强烈推荐先读 ...

  6. python中的数据类型与json的数据类型之间的转化

    什么是json: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programm ...

  7. iOS10个实用小技巧(总有你不知道的和你会用到的)

    本文转载至 http://www.jianshu.com/p/a3156826c27c 在开发过程中我们总会遇到各种各样的小问题,有些小问题并不是十分容易解决.在此我就总结一下,我在开发中遇到的各种小 ...

  8. 机器学习算法--GBDT

    转自 http://blog.csdn.net/u014568921/article/details/49383379 另外一个很容易理解的文章 :http://www.jianshu.com/p/0 ...

  9. 【EF框架】使用params参数传值防止SQL注入报错处理

    通过SqlParameter传时间参数,代码如下: var param = new List<SqlParameter>(); param.Add(new SqlParameter(&qu ...

  10. 非IMU模式下DML语句产生的REDO日志内容格式解读

    实验内容:非IMU模式下DML语句产生的REDO日志内容格式解读 最详细的解读是UPDATE的. 实验环境准备 11G中默认是开启IMU特性的,做此实验需要关闭此特性. alter system se ...