双射 - hash去重
题目描述
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.
题意 : 给你两个大小为 N 的图,要求寻找既是B的子图,又和A是同构的图的数量
思路分析:刚开始一直不是很懂它的题意是要干嘛,比赛后也看了很长时间才懂一些,1-2 3 和 1 2-3 是属于同构关系的,因为都是两个点连着,一个点单独出现,因此这里只要N!枚举对应一下就可以了
1 . hash去判重
#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
const ll maxn = 1e6+5;
const ll mod = 1e9+7;
const double eps = 1e-9;
const double pi = acos(-1.0);
const ll inf = 0x3f3f3f3f; ll n, m1, m2;
bool mp1[10][10], mp2[10][10];
ll arr[10], d[10];
set<ll>s; void solve() {
for(ll i = 1; i <= n; i++) arr[i] = i; do{
for(ll i = 1; i <= n; i++) d[i] = arr[i]; ll sum = 0;
for(ll i = 1; i <= n; i++){
for(ll j = i+1; j <= n; j++){
if (mp2[i][j] && mp1[d[i]][d[j]]){
sum++;
}
}
}
if (sum == m1){
ll x = 0;
for(ll i = 1; i <= n; i++){
for(ll j = i+1; j <= n; j++){
if (mp1[d[i]][d[j]]) {
x+=(1LL<<(i*(n-1)+j));
}
}
}
//printf("++++ %llu \n", x);
s.insert(x);
}
}while(next_permutation(arr+1, arr+1+n));
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
ll a, b; while(~scanf("%d%d%d", &n, &m1, &m2)){
memset(mp1, false, sizeof(mp1));
memset(mp2, false, sizeof(mp2)); for(ll i = 1; i <= m1; i++){
scanf("%d%d", &a, &b);
mp1[a][b] = mp1[b][a] = true;
}
for(ll i = 1; i <= m2; i++){
scanf("%d%d", &a, &b);
mp2[a][b] = mp2[b][a] = true;
}
s.clear();
solve();
printf("%d\n", s.size());
}
return 0;
}
2 . 直接这样考虑,就是A图自身的同构在通过B去计算的时候,会重复算的,因此除一下就可以了
using namespace std;
#define ll unsigned long long
const ll maxn = 1e6+5;
const ll mod = 1e9+7;
const double eps = 1e-9;
const double pi = acos(-1.0);
const ll inf = 0x3f3f3f3f; ll n, m1, m2;
bool mp1[10][10], mp2[10][10];
ll arr[10], d[10];
set<ll>s;
int a1, a2; void solve() {
for(ll i = 1; i <= n; i++) arr[i] = i; do{
for(ll i = 1; i <= n; i++) d[i] = arr[i]; ll sum = 0;
for(ll i = 1; i <= n; i++){
for(ll j = i+1; j <= n; j++){
if (mp2[i][j] && mp1[d[i]][d[j]]){
sum++;
}
}
}
if (sum == m1){
a1++;
}
}while(next_permutation(arr+1, arr+1+n));
} void solve2() {
for(ll i = 1; i <= n; i++) arr[i] = i; do{
for(ll i = 1; i <= n; i++) d[i] = arr[i]; ll sum = 0;
for(ll i = 1; i <= n; i++){
for(ll j = i+1; j <= n; j++){
if (mp1[i][j] && mp1[d[i]][d[j]]){
sum++;
}
}
}
if (sum == m1){
a2++;
}
}while(next_permutation(arr+1, arr+1+n));
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
ll a, b; while(~scanf("%d%d%d", &n, &m1, &m2)){
memset(mp1, false, sizeof(mp1));
memset(mp2, false, sizeof(mp2)); for(ll i = 1; i <= m1; i++){
scanf("%d%d", &a, &b);
mp1[a][b] = mp1[b][a] = true;
}
for(ll i = 1; i <= m2; i++){
scanf("%d%d", &a, &b);
mp2[a][b] = mp2[b][a] = true;
}
s.clear();
a1 = a2 = 0;
solve();
solve2();
printf("%d\n", a1/a2);
}
return 0;
}
双射 - hash去重的更多相关文章
- 倒排索引 获取指定单词的文档集合 使用hash去重单词term 提高数据压缩率的方法
倒排索引源于实际应用中需要根据属性的值来查找记录.这种索引表中的每一项都包括一个属性值和具有该属性值的各记录的地址.由于不是由记录来确定属性值,而是由属性值来确定记录的位置,因而称为倒排索引(inve ...
- ACM-ICPC 2018 南京赛区网络预赛 I Skr (马拉车+hash去重)或(回文树)
https://nanti.jisuanke.com/t/30998 题意 给一串由0..9组成的数字字符串,求所有不同回文串的权值和.比如说“1121”这个串中有“1”,“2”,“11”,“121” ...
- JavaScript-数组去重由慢到快由繁到简
indexOf去重 Array.prototype.unique1 = function() { var arr = []; for (var i = 0; i < this.length; i ...
- BZOJ 2761 不重复数字 (Hash)
题解:直接使用STL中的hash去重即可 #include <cstdio> #include <map> using namespace std; int ans[50010 ...
- js引用类型数组去重-对象标记法
前言 Js数组去重已经有很多种实现方式:包括逐个检索对比(使用Array.property.indexOf),先排序后对比,使用hash表,利用ES6中的Set()等.这些数组去重办法中速度最快的是h ...
- JavaScript 数组去重方法总结
1.遍历数组法: 这应该是最简单的去重方法(实现思路:新建一新数组,遍历数组,值不在新数组就加入该新数组中) // 遍历数组去重法 function unique(arr){ var _arr = [ ...
- 160819、JavaScript-数组去重由慢到快由繁到简
JavaScript-数组去重由慢到快由繁到简演化 indexOf去重 Array.prototype.unique1 = function() { var arr = []; for (var ...
- 关于distinct 和group by的去重逻辑浅析
在数据库操作中,我们常常遇到需要将数据去重计数的工作.例如: 表A,列col A C A B C D A B 结果就是一共出现4个不同的字母A.B.C.D 即结果为4 大体上我们可以选择count(d ...
- XIII Open Cup named after E.V. Pankratiev. GP of America
A. Explosions 注意到将炸弹按坐标排序后,每个炸弹直接引爆和间接引爆的都是连续的一段区间,因此只需要求出每个炸弹能间接炸到的最左和最右的炸弹即可. 建立图论模型,炸弹$i$向炸弹$j$连单 ...
随机推荐
- ERROR 1366 (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 'name' at row 1
ERROR 1366 (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 'name' at row 1 数据库字符集问 ...
- P1061 最长连号
题目描述 输入n个正整数,(1<=n<=10000),要求输出最长的连号的长度.(连号指从小到大连续自然数) 输入格式 第一行,一个数n; 第二行,n个正整数,之间用空格隔开. 输出格式 ...
- 【js】react-native Could not find iPhone 6 simulator 和 Entry, ":CFBundleIdentifier", Does Not Exist 两种报错解决办法
一.在运行rn app应用时,react-native run:ios 报错出现 Could not find iPhone 6 simulator 解决办法: 1.react-native r ...
- Git用在公司项目上的操作
修改代码后再次提交 搭档优化好他自己的代码后,我想在vscode上看看他优化后的结果 此时直接git pull origin就可以看到了 j接下来的一些指令,慢慢了解... 分支本身已经在我上面 以下 ...
- 【t050】方程求解
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 要求Xi(i = 1,2,3,4)是一个[-T..T]中的整数,满足方程AX1 + BX2 + CX3 ...
- (转)学习C语言基本思路与参考书籍
计算机行业发展非常快,大学里的教育基本都跟不上实际的社会需求.如果你所在的学校还在指定大家使用谭浩强的教材,或使用VC6.0来教大家上机实验,那你不妨看看本文,这里有一些建议可以帮助你不会脱离社会太远 ...
- Vue CLI 介绍安装
https://cli.vuejs.org/zh/guide/ 介绍 警告 这份文档是对应 @vue/cli 3.x 版本的.老版本的 vue-cli 文档请移步这里. Vue CLI 是一个基于 V ...
- 从头学pytorch(三) 线性回归
关于什么是线性回归,不多做介绍了.可以参考我以前的博客https://www.cnblogs.com/sdu20112013/p/10186516.html 实现线性回归 分为以下几个部分: 生成数据 ...
- jdbc链接Oracle数据库的封装
在src下创建properties文件 driver=oracle.jdbc.driver.OracleDriverurl=jdbc:oracle:thin:@//127.0.0.1:1521/XEu ...
- 我们基于kaldi开发的嵌入式语音识别系统升级成深度学习啦
先前的文章<三个小白是如何在三个月内搭一个基于kaldi的嵌入式在线语音识别系统的>说我们花了不到三个月的时间搭了一个基于kaldi的嵌入式语音识别系统,不过它是基于传统的GMM-HMM的 ...