题目链接

题意 : 给出两幅顶点数一样的图 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 ( 图的同构 )的更多相关文章

  1. USTC 1119 graph 图的同构

    USTC 1119 图的同构的严格定义可以参考离散数学:The simple graphs G1=(V1,E1) and G2=(V2,E2)are isomorphic if there exist ...

  2. 2018牛客网暑期ACM多校训练营(第一场) D - Two Graphs - [无向图同构]

    题目链接:https://www.nowcoder.com/acm/contest/139/D 题目描述 Two undirected simple graphs  and  where  are i ...

  3. Two Graphs 牛客网暑期ACM多校训练营(第一场)D 图论基础知识 全排列

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 Two undirected simple graphs and where are isomo ...

  4. tunning-Instruments and Flame Graphs

    On mac os, programs may need Instruments to tuning, and when you face too many probe messages, you'l ...

  5. 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 ...

  6. 特征向量-Eigenvalues_and_eigenvectors#Graphs

    https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors#Graphs A               {\displaystyle A} ...

  7. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  8. NowCoder猜想(素数筛法+位压缩)

    在期末被各科的大作业碾压快要窒息之际,百忙之中抽空上牛客网逛了逛,无意中发现一道好题,NowCoder猜想,题意很明显,就是个简单的素数筛法,但竟然超内存了,我晕(+﹏+)~  明明有 3 万多 k ...

  9. [nowCoder] 两个不等长数组求第K大数

    给定两个有序数组arr1和arr2,在给定一个整数k,返回两个数组的所有数中第K小的数.例如:arr1 = {1,2,3,4,5};arr2 = {3,4,5};K = 1;因为1为所有数中最小的,所 ...

随机推荐

  1. 2019牛客暑期多校训练营(第一场)-A (单调栈)

    题目链接:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个长度均为n的数组a和b,求最大的p使得(a1,ap)和(b1,bp)等价,等价的定义为其任意 ...

  2. coredump产生的几种可能情况

    coredump产生的几种可能情况 造成程序coredump的原因有很多,这里总结一些比较常用的经验吧: 1,内存访问越界 a) 由于使用错误的下标,导致数组访问越界. b) 搜索字符串时,依靠字符串 ...

  3. [转帖]Java 8新特性探究(八)精简的JRE详解

    Java 8新特性探究(八)精简的JRE详解 https://my.oschina.net/benhaile/blog/211804 精简版的api   撸了今年阿里.网易和美团的面试,我有一个重要发 ...

  4. Eclipse myeclipse下配置HanLP的教程

    一.说明 博主的配置 1:window10 2:myeclipse 3:jdk1.8 备注:文章分享自贾继康的博客,博客使用的hanlp是1.6.8的版本.大家可以去下载最新的1.7版本了,也比较推荐 ...

  5. gz文件的压缩和解压

    gz文件的压缩和解压 压缩: gzip filename 解压: gunzip filename.gz

  6. JavaScript中好用的对象数组去重

    对象数组去重 Demo数据如下: var items= [{ "specItems": [{ "id": "966480614728069122&qu ...

  7. 云数据库 MariaDB 版

    基于MariaDB企业版全球独家合作认证,提供Oracle兼容性及众多企业级数据库特性.支持包括MySQL InnoDB等多种存储引擎,为不同需求的用户提供灵活的选择. 请看视频简介 优势 Oracl ...

  8. 5表联查yii框架权限控制

    一:控制器部分 <?php namespace app\controllers; use yii\web\Controller; class PreController extends Cont ...

  9. Ruby学习中(首探数组, 注释, 运算符, 三元表达式, 字符串)

    一. 数组 1.定义一个数组 games = ["英雄联盟", "绝地求生", "钢铁雄心"] puts games 2.数组的循环 gam ...

  10. ubuntu 创建定时备份pg数据库定时任务

    ubuntu创建定时备份数据库定时任务 一.命令文件 创建db_back.sh #!/bin/bash echo "start backup" /usr/lib/postgresq ...