同构的判断 (全排列+状压)

Two Graphs

时间限制: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} ∈ 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的所有可能可能情况,用全排列枚举,状压存下来,再放在一个set去重

代码:

#include<bits/stdc++.h>
using namespace std;
int n,m2,m1;
#define MAX 10
#define pii pair<int,int>
typedef long long ll;
int G1[MAX][MAX],G2[MAX][MAX];
map<pii,int>mp;
set<ll>s;
int main()
{
    while(cin>>n>>m1>>m2){
            mp.clear();
            s.clear();
            memset(G1,0,sizeof(G1));
            memset(G2,0,sizeof(G2));
    for(int i=0;i<m1;i++)
    {
        int u,v;
        cin>>u>>v;
        G1[u][v]=G1[v][u]=1;
    }
    for(int i=0;i<m2;i++)
    {
        int u,v;
        cin>>u>>v;
        G2[u][v]=G2[v][u]=1;
         if(u<v)swap(u,v);
        mp[make_pair(u,v)]=i;
    }
    ll ans[MAX];
    for(int i=1;i<=n;i++)
        ans[i]=i;
    do{
        ll state=0;
        bool ok=true;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(G1[i][j]&&!G2[ans[i]][ans[j]])
                {
                    ok=false;
                    break;
                }
                else
                {
                    if(G1[i][j])
                    {
                        int u=ans[i];
                        int v=ans[j];
                        if(u<v)swap(u,v);
                        state|=(1LL<<mp[make_pair(u,v)]);
                    }
                }
            }
            if(!ok)break;
        }
        if(!ok)continue;
        s.insert(state);//cout<<s.size()<<"00000"<<endl;
    }while(next_permutation(ans+1,ans+1+n));
    cout<<s.size()<<endl;
    }
}

 

转载于:https://www.cnblogs.com/zhgyki/p/9438017.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值