牛客网多校练习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;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值