牛客网暑期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());
    }
}
发布了10 篇原创文章 · 获赞 1 · 访问量 355

猜你喜欢

转载自blog.csdn.net/hl_1997/article/details/81131024
今日推荐