牛客网多校训练第一场D题图论 暴力 +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.

 

输入

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

输出

2
3

题意求G2的子图与G1 的同构的个数

方法:暴力

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
const int maxn=1e2+5;
const int INF=0x3f3f3f3f;
bool vis[maxn];
int n;
int m1,m2;
map<long long,int> mp;
struct edge
{
    int from,to;
}d1[maxn],d2[maxn];
int G1[maxn][maxn];
int G2[maxn][maxn];
int a[maxn];
int ans;
int main()
{
    while(~scanf("%d%d%d",&n,&m1,&m2))
    {
    memset(G1,0,sizeof G1);
    memset(G2,0,sizeof G2);
    mp.clear();
    int u,v;
    for(int i=1;i<=m1;i++)
        scanf("%d%d",&u,&v),G1[u][v]=G1[v][u]=1;
    for(int i=1;i<=m2;i++)
        scanf("%d%d",&u,&v),G2[u][v]=G2[v][u]=i;//给每个边打上编号,便于后面去重
    for(int i=1;i<=n;i++) a[i]=i;
    ans=0;
    do
    {
        int flag=0;
        long long x=0;
        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
                if(G1[i][j])//把一个点a[i],a[j](经过排序之后)映射到边G1[i][j]上去
                {
                    if(G2[a[i]][a[j]]==0) flag=1;//G2中点a[i],a[j]直接没有边
                    else   x|=1<<G2[a[i]][a[j]];//x是每个边的id与运算的结果
    //如果一个子图中两条边是一样的,那么就是重复的情况,就要保证每个子图的id不能全一样
                }
        if(flag||mp.count(x)) continue;
        mp[x]=1;
        ans++;

    }while(next_permutation(a+1,a+1+n));//枚举点的全排列
    printf("%d\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值