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 ...
随机推荐
- xcode 5.1打包iOS 7.1应用问题笔记
XCODE 5.1默认情况下是要求应用都通过64位编译.但是往往有些第三方的类库还是32位.还木有更新64位类库.使得项目编译出错. 解决办法: BuildSetting 的Valid Archite ...
- c# 匿名反序列化
1.先new一个匿名对象,然后再反序列化好处是能点点点,坏处是得先new匿名对象 2.借用Newtonsoft.Json.Linq.JObject.Parse,好处是不需要new匿名对象,坏处是不能点 ...
- 线程同步 –Mutex和Semaphore
上一篇介绍了同步事件EventWaitHandle,以及它的两个子类型AutoResetEvent和ManualResetEvent.下面接着介绍WaitHandle的另外两个子类型Mutex和Sem ...
- Spring容器AOP的理解
一句话理解:根据被代理对象信息通过Proxy动态生成我们具体的代理类. 实现就动态代理.那动态代理是什么呢? 动态代理其实并不是什么新鲜的东西,学过设计模式的人都应该知道代理模式,代理模式就是一种静态 ...
- Flash XSS 漏洞实例
www.bsdxm.com/zeroclipboard/ZeroClipboard.swf?id=\"))}catch(e){alert(/xss/);}//&width=500&a ...
- python cookie
http://www.jayconrod.com/posts/17/how-to-use-http-cookies-in-python
- inux跟踪线程的方法:LWP和strace命令
摘要:在使用多线程程序时,有时会遇到程序功能异常的情况,而这种异常情况并不是每次都发生,很难模拟出来.这时就需要运用在程序运行时跟踪线程的手段,而linux系统的LWP和strace命令正是这种技术手 ...
- 汉字按首字母排序(javascript,php,mysql实现)
1.javascript实现 var a = ["啊","得啊_123","得啊_0124","波啊","婆& ...
- UML类图的几种关系总结【转】
在UML类图中,常见的有以下几种关系: 泛化(Generalization), 实现(Realization),关联(Association),聚合(Aggregation),组合(Composit ...
- web基础----->jersey整合jetty开发restful应用(一)
这里介绍一个jersey与jetty整合开发restful应用的知识.将过去和羁绊全部丢弃,不要吝惜那为了梦想流下的泪水. jersey与jetty的整合 一.创建一个maven项目,pom.xml的 ...