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

题意:给出两张图G1,G2,问G2有多少个子图与G1是同构的

思路:最多只有8个点,所以可以把所有的映射关系一一枚举出来,然后在判断是否与G1同构,G1中有的每一条边,该边的两个点所映射过去G2的两个点也必须存在一条边,因为可能有重复的情况,用了状压的思想来去重

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>

using namespace std;
int mp1[10][10],mp2[10][10];
int a[10];
map<int,int> mp;

int main()
{
    int n,m1,m2;
    while(scanf("%d%d%d",&n,&m1,&m2) != EOF) {
        for(int i = 1; i <= n; i++) a[i] = i;
        mp.clear();
        memset(mp1,0,sizeof(mp1));
        memset(mp2,0,sizeof(mp2));
        int x,y;
        for(int i = 1; i <= m1; i++) {
            scanf("%d%d",&x,&y);
            mp1[x][y] = mp1[y][x] = 1;
        }
        for(int i = 1; i <= m2; i++) {
            scanf("%d%d",&x,&y);
            mp2[x][y] = mp2[y][x] = i;
        }
        int ans = 0;
        do {
            int flag = 1,b = 0;
            for(int i = 1; i <= n; i++) {
                for(int j = 1; j <= n; j++) {
                    if(mp1[i][j]) {
                        if(mp2[a[i]][a[j]] == 0) flag = 0;
                        else b |= 1 << mp2[a[i]][a[j]];
                    }
                }
            }
            if(flag == 1 && mp[b] == 0) {
                mp[b] = 1;
                ans++;
            }
        }while(next_permutation(a + 1,a + 1 + n));
        printf("%d\n",ans);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值