2018牛客多校第一场 Two Graphs(图的同构+全排列映射)

链接:https://www.nowcoder.com/acm/contest/139/D
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld

题目描述

Two undirected simple graphs and where are isomorphic when there exists a bijection on V satisfying  if and only if {x, y} ∈ E 2.
Given two graphs and , count the number of graphs satisfying the following condition:
* .
* G 1 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, m
1
 and m
2
 where |E
1
| = m
1
 and |E
2
| = m
2
.
The i-th of the following m
1
 lines contains 2 integers a
i
 and b
i
 which denote {a
i
, b
i
} ∈ E
1
.
The i-th of the last m
2
 lines contains 2 integers a
i
 and b
i
 which denote {a
i
, b
i
} ∈ E
2
.

输出描述:

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 ≤ a
i
, b
i
 ≤ n
* The number of test cases does not exceed 50.


图的同构的概念: 离散数学:图的同构
因为n的范围较小,所以直接全排列枚举映射,对图中的每一条边映射一个值,这样的一组集合就能代表一种图的同构
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> p;
typedef long long ll;
map<p,int>mp1;
map<p,int>mp2;
map<int,int>mp;
set<int>se;
const int mod=1e6+7;
const int N=1e6+10;
int a[10];
int main(){
    int n,m1,m2,x,y;
    while(~scanf("%d%d%d",&n,&m1,&m2)){
        mp1.clear();mp2.clear();mp.clear();se.clear();
        for(int i=1;i<=m1;i++){
            scanf("%d%d",&x,&y);mp1[{x,y}]=i;se.insert(x);se.insert(y);
        }
        for(int i=1;i<=m2;i++){
            scanf("%d%d",&x,&y);mp2[{x,y}]=i;
        }  
        int ans=0;
        for(int i=1;i<=n;i++) a[i]=i;
        set<ll>ss;ss.clear();
        do{
            mp.clear(); int flag=1;
            for(int i=1;i<=n;i++) mp[i]=a[i];ll tmp=0;
            for(auto it=mp1.begin();it!=mp1.end();it++){
                p a=it->first;p b1={mp[a.first],mp[a.second]};
                p b2={mp[a.second],mp[a.first]};
                if(mp2[b1]!=0){ tmp+=(1<<mp2[b1]);}
                else if(mp2[b2]!=0){tmp+=(1<<mp2[b2]);}
                else flag=0;
            }
            if(flag) ss.insert(tmp);
        }while(next_permutation(a+1,a+n+1));
        printf("%d\n",ss.size());
    }
    return 0;
}
View Code




转载于:https://www.cnblogs.com/vainglory/p/9674626.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值