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

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