Two Graphs 牛客网暑期ACM多校训练营(第一场) (子图同构+hash+暴力)

链接: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.

示例1
输入
复制

3 1 2
1 3
1 2
2 3
4 2 3
1 2
1 3
4 1
4 2
4 3

输出
复制

2
3

题意:找出一个图与另一个图同构的子图个数
思路: 要判断是否同构,就需要把原图映射一个成新图,这个新图相对结构不变,就只有图的编号的顺序被打乱了而已,然后在与另一个图去一一比较边的对应位置,我们这里因为点是一样的,所以我们先去确定点的对应顺序,然后再去判断是否边一一对应,全排列即可,然后去下重就行

accode

#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
#define ULL unsigned long long
using namespace std;
const int maxn = 10;
int n,m1,m2;
int va[10];
int ma1[10][10];
int ma2[10][10];
int main()
{
    while(~scanf("%d%d%d",&n,&m1,&m2))
    {
        memset(ma1,0,sizeof(ma1));
        memset(ma2,0,sizeof(ma2));
        for(int i = 1; i<=n; i++)
        {
            va[i] = i;
        }
        for(int i = 0; i<m1; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            ma1[u][v] = 1;
            ma1[v][u] = 1;
        }
        for(int i = 0; i<m2; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            ma2[u][v] = 1;
            ma2[v][u] = 1;
        }
        set<ULL>S;
        do
        {
            ULL tmp = 0;
            int falg = 1;
            for(int i = 1; i<=n; i++)
            {
                for(int j = i+1;j<=n;j++){
                    if(ma1[i][j]==1&&ma2[va[i]][va[j]] == 1){
                            int xx  = va[i];
                            int yy = va[j];
                            if(xx>yy){
                                swap(xx,yy);
                            }
                            tmp |=  ((ULL)1<<(xx*n+yy));
                    }
                    else if(ma1[i][j]==1&&ma2[va[i]][va[j]]==0){
                        falg = 0;
                        break;
                    }
                }
                if(falg==0){
                    break;
                }
            }
            if(falg){
               // cout<<tmp<<endl;
                S.insert(tmp);
            }
        }while(next_permutation(va+1,va+1+n));
        cout<<S.size()<<endl;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值