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 ...
随机推荐
- 5 -- Hibernate的基本用法 --4 8 外连接抓取属性
外连接抓取能限制执行SQL语句的次数来提高效率,这种外连接抓取通过在单个select语句中使用outer join来一次抓取多个数据表的数据. 外连接抓取允许在单个select语句中,通过@ManyT ...
- 一段代码让DedeCMS完美兼容PHP5.4
DedeCMS V5.7版本,在本地部署后,正确登录后台的情况下页面没有任何输出和显示(错误登录或密码错误时才有显示),也没有报错.进到脚本调试,发现问题出在userLogin类所在的脚本userlo ...
- Git Step by Step – (8) Git的merge和rebase
前面一篇文章中提到了"git pull"等价于"git fetch"加上"git merge",然后还提到了pull命令支持rebase模式 ...
- Eclipse cdt debug时‘Error while launching command: gdb.exe --version’
1. 下载gdb,网上很多可以下载一个,解压放在mingw/bin下,由于该目录以在path制定,在CMD下,gdb -version会显示当前gdb版本信息. 2.按照该文档配置即可实现debug
- 在linux下搭建ftp服务器【转】
1 安装 vsftpd yum install vsftpd 2 配置 vsftpd 打开 vsftpd 文件: vi /etc/vsftpd/vsftpd.conf 初次修改前建议备份该文件 2.1 ...
- UITableView-FDTemplateLayoutCell 学习笔记
本文转载至 http://www.tuicool.com/articles/I7ji2uM 原文 http://everettjf.github.io/2016/03/24/learn-uitabl ...
- 【Python3】 django2.0 url 跳转设置
python: 3.6.4 django : 2.0 在创建应用时候.我是把 urls.py 分开了.所以在设置url跳转时候.要修改成如下模式 1 父 urls.py 里边要加上命名空间 2 ...
- 【大数据系列】hadoop2.0中的jobtracker和tasktracker哪里去了
低版本的hadoop下MapReduce处理流程 1.首先用户程序(JobClient)提交了一个job,job的信息会发送到Job Tracker,Job Tracker是Map-reduce框架的 ...
- 如何使用 Flexbox 和 CSS Grid,实现高效布局
CSS 浮动属性一直是网站上排列元素的主要方法之一,但是当实现复杂布局时,这种方法不总是那么理想.幸运的是,在现代网页设计时代,使用 Flexbox 和 CSS Grid 来对齐元素,变得相对容易起来 ...
- jQuery Sizzle选择器(二)
自己开始尝试读Sizzle源码. 1.Sizzle同过自执行函数的方式为自己创建了一个独立的作用域,它可以不依赖于jQuery的大环境而独立存在.因此它可以被应用到其它js库中.实现如下:(fun ...