2018牛客多校训练--Two Graphs(DFS)

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

 两个图点的个数相同,问图二的子图有多少个是图一的同构。

  思路:

  数据量比较小直接暴力搜索。通过查找边,记录边的路径,看能否找到同构。

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N=10;

int n,m1,m2;
int look[N][N],vis[N][N],pre[N],w[N],ans;
map<ll,int>mp;

struct Node
{
    int x,y;
}node[N];

void dfs(int rt)
{
    ll pos=0,i;
    if(rt==n+1)
    {
        for(i=1;i<=m1;i++)//查找图一的每一条边是否都能在图二中找到映射
        {
            if(vis[pre[node[i].x]][pre[node[i].y]]==0)
                return;
            pos|=(1<<vis[pre[node[i].x]][pre[node[i].y]]);//记录路径
        }
        if(mp[pos])
            return;
        mp[pos]=1;
        ans++;
        return;
    }
    for(int i=1;i<=n;i++)//搜索图二中的每一个点
    {
        if(w[i])
            continue;
        pre[rt]=i;
        w[i]=1;
        dfs(rt+1);
        w[i]=0;
    }
}

int main()
{
    int i,j;
    while(~scanf("%d%d%d",&n,&m1,&m2))
    {
        mp.clear();
        memset(look,0,sizeof(look));
        memset(vis,0,sizeof(vis));
        memset(w,0,sizeof(w));
        ans=0;
        for(i=1;i<=n;i++)
            pre[i]=i;//两个图点之间的映射关系
        for(i=1;i<=m1;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            look[x][y]=look[y][x]=1;
            node[i].x=x;
            node[i].y=y;
        }
        for(i=1;i<=m2;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            vis[x][y]=vis[y][x]=i;
        }
        dfs(1);
        printf("%d\n",ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值