牛客网暑期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.

 

示例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

备注:

* 1 ≤ n ≤ 8
* 
* 1 ≤ ai, bi ≤ n
* The number of test cases does not exceed 50.

题意:找出G2子图与G1同构的个数

思路:通过对点的全排列,然后判断在G2中的边,在当时的排列中G1的两点是否包含边,就是要G1的边与G2去对应,然后就会有多种情况,然后可以通过hash去重。

 

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=100000+10;
const double eps=1e-15;
const int mod=1e9+7;
typedef pair<int,int> P;
int n,m1,m2,num,a1,b1,z,k,map1[9][9],a[9];
set<ll>s;
P map2[30];
void init()
{
    memset(map1,0,sizeof(map1));
    for(int i=1;i<=n;i++)
        a[i]=i;
    s.clear();
}
int main()
{
    while(~scanf("%d%d%d",&n,&m1,&m2))
    {
        init();
        int u,v;
        for(int i=1;i<=m1;i++)
        {
            scanf("%d%d",&u,&v);
            map1[u][v]=map1[v][u]=1;
        }
        for(int i=1;i<=m2;i++)
            scanf("%d%d",&map2[i].first,&map2[i].second);
        do{
                int cnt=0;
                ll hashs=0;
                for(int i=1;i<=m2;i++)//枚举G2的边
                    if(map1[a[map2[i].first]][a[map2[i].second]])//在G2中的边与G1边的对应
                    {
                        hashs=hashs*101+i;
                        hashs%=mod;
                        cnt++;
                    }
                if(cnt==m1)
                    s.insert(hashs);
        }while(next_permutation(a+1,a+n+1));//对G1进行全排列
        printf("%d\n",s.size());
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值