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为所有数中最小的,所 ...
随机推荐
- 【Python】【demo实验3】【显示素数,显示两个数范围内的所有素数】
打印两个整数之间的所有素数: (使用平方根来判断 是否应停止验证该数值是否为素数) for i in range(956253526252,9956253526252): k = 1 if i == ...
- Kick Start 2019 Round A Parcels
题目大意 $R \times C$ 的网格,格子间的距离取曼哈顿距离.有些格子是邮局.现在可以把至多一个不是邮局的格子变成邮局,问每个格子到最近的邮局的曼哈顿距离的最大值最小是多少. 数据范围 $ 1 ...
- 最短meeting路线(树的直径)--牛客第四场(meeting)
题意: 给你一棵树,树上有些点是有人的,问你选一个点,最短的(最远的那个人的距离)是多少. 思路: 其实就是树的直径,两遍dfs,dfs第二遍的时候遇到人就更新直径就行了,ans是/2,奇数的话+1. ...
- Windows 窗体消息大全(速查)
Windows窗口消息大全,全不全自己撸 通用窗口消息 WM_NULL:--------->空消息,可检测程序是否有响应等 WM_CREATE:--------->新建一个窗口 WM_DE ...
- python+minicap的使用
说起Minicap,不得不提到STF,STF (Smartphone Test Farm) 是一个开源的web架构应用,用户可通过浏览器远程操作Android设备.调试Android应用.在设备上进行 ...
- C# 面向对象3 静态和非静态的区别
静态和非静态的区别 1.在非静态类中,既可以有实例成员(非静态成员),也可以有静态成员. 2.在调用实例成员的时候,需要使用对象名.实例成员; 在调用静态成员的时候,需要使用类名.静态成员名; 总结: ...
- Oracle Help 类
public static string ConnString = @"Data Source=xxx;USER ID=xxx;PASSWORD=xxx"; /// <sum ...
- 基于Websocket的websocketd
WebSocket是什么 WebSocket是HTML5下面的一种技术,设计出来的目的就是要取代轮询和 Comet 技术,使客户端浏览器具备像 C/S 架构下桌面系统的实时通讯能力. 浏览器通过 Ja ...
- HTML5的新变化
1.新的html5文件类型,仅需申明在html的第一行,即 <!DOCTYPE html> 2.图形元素 figure ,将<figure>与<figcaption> ...
- CentOS7 ab压力测试安装
①.ab(apache benchmark)安装 命令: yum -y install httpd-tools ②.ab测试的命令参数 命令: ab 或 ab -help 显示命令参数如下 ③.ab的 ...