牛客网多校练习1 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.

示例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;
#define ll long long

int n,m1,m2;
struct edge{ int a , b ; };
using graph=vector<edge> ;
/*
题目大意:给定两张图,a,b,
求在图b中和a 同构的子图个数。

关于同构的判断处理方法是:
把下标进行排列映射,带入判断。
*/
graph read(int m)
{
    graph ans;
    for(int i=0,a,b;i<m;i++)
    {
        scanf("%d%d",&a,&b);  a--,b--;
        ans.push_back( edge{a,b} );
    }
    return ans;
}

int Count(int n,graph &a,graph &b)
{
    vector< vector<bool> > target( n,vector<bool>(n,false) );
    for(auto && e : b )  target[e.a][e.b]=target[e.b][e.a]=true;

    vector<int> phi(n);
    iota(phi.begin(),phi.end(),0);///利用了函数iota方法,批量递增

    int ans=0;
    do{
        bool k=true;
        for(auto &&e :a) k&=target[phi[e.a]][phi[e.b]];
        ans+=k;
    }while(next_permutation( phi.begin() , phi.end() ) );
    return ans;
}

int main()
{
    while(~scanf("%d%d%d",&n,&m1,&m2))
    {
        graph A=read(m1);
        graph B=read(m2);

        int isab=Count(n,A,B);
        int isaa=Count(n,A,A);///记得要去除自同构的个数
        printf("%d\n",isab/isaa);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/81252380
今日推荐