2018牛客网暑期ACM多校训练营(第一场) D - Two Graphs - [无向图同构]
题目链接: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 - [无向图同构]的更多相关文章
- 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)
2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...
- 2018牛客网暑期ACM多校训练营(第一场)D图同构,J
链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...
- 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)
Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...
- 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)
链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...
- 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)
链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...
- 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)
题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...
- 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)
题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...
- 2018牛客网暑期ACM多校训练营(第三场) A - PACM Team - [四维01背包][四约束01背包]
题目链接:https://www.nowcoder.com/acm/contest/141/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 2018牛客网暑期ACM多校训练营(第五场) F - take - [数学期望][树状数组]
题目链接:https://www.nowcoder.com/acm/contest/143/F 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]
题目链接:https://www.nowcoder.com/acm/contest/143/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
随机推荐
- 车牌、手机、身份证、等敏感信息 屏蔽 替换 、中文转unicode编码 函数
应工作要求,需要对展示的内容进行敏感信息替换.琢磨的一些时间,编写的函数匹配率还是比较高的. 顺便说下思路,使用的是正则匹配替换和字符串替换.函数可以再改进. 先把需要匹配的内容写好相应的正则,然后进 ...
- python的输出问题
我们知道python提供了一个shell来供初学者学习,在shell里是输入一句执行一句,例如:
- Ansible的快速入门
Ansible 是一个简单的自动化引擎,可完成配置管理,应用部署,服务编排等各种IT需求. Ansible使用python语言开发实现的开源软件,依赖于Jinjia2,paramiko和PyYAML这 ...
- hadoop完全分布式搭建HA(高可用)
2018年03月25日 16:25:26 D调的Stanley 阅读数:2725 标签: hadoop HAssh免密登录hdfs HA配置hadoop完全分布式搭建zookeeper 配置 更多 个 ...
- 解决安装laravel/homestead vagrant环境报"A VirtualBox machine with the name 'homestead' already exists."的错误
之前在mac上安装laravel/homestead vagrant虚拟机环境时由于参照的教程是: 每次都必须在~/Homestead目录下边运行vagrant up/halt命令,觉得实在是不方便, ...
- 【Nginx】服务器中HTTP 301跳转到带www的域名的方法
从nginx的官方文档 documentation, 正确的nginx https 301跳转到带www域名方法的方法如下: HTTP 301跳转到带www域名方法 需要两个server段. serv ...
- 【大数据系列】hadoop单机模式安装
一.添加用户和用户组 adduser hadoop 将hadoop用户添加进sudo用户组 sudo usermod -G sudo hadoop 或者 visudo 二.安装jdk 具体操作参考:c ...
- Tomorrow Is A New Day
Sometimes we do not feel like we want to feel Sometimes we do not achieve what we want to achiev ...
- Android NDK学习(4)使用cygwin生成.so库文件
转:http://www.cnblogs.com/fww330666557/archive/2012/12/14/2817389.html 简单的示例: makefile文件: LOCAL_PATH: ...
- vue生成路由实例, 使用单个vue文件模板生成路由
一.vue-loader与vue-router配合 $ cnpm install vue-router --save 二.生成vue-webpack模板 $ vue init webpack-simp ...