2018牛客网暑期ACM多校训练营(第一场)D图同构,J
链接:https://www.nowcoder.com/acm/contest/139/D
来源:牛客网
同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所有的x,y∈V均有xy∈E等价于m(x)m(y)∈E1,则称G和G1是同构的,这样的一个映射m称之为一个同构,如果G=G1,则称他为一个自同构。
题目描述
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.
输入
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. 题意 两个简单无向图,g1,g2.问g2的子图中有多少个是g1的同构图
解析 点的数量是8我们不能用边来枚举子图 数量太多了 我们可以把点全排列按照映射的关系去找边是否存在 再把重复的去掉就是答案
去重可以用二进制压缩边集set去重,也可以哈希,除以自同构数量,暴力。。。。
自同构数求法 枚举全排列映射到本身g1 如果图完全一样 计下数。
代码:
#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f,maxn=,mod=1e9+;
typedef long long ll;
int n,m1,m2,u[maxn*],v[maxn*],g1[maxn][maxn],g2[maxn][maxn],a[maxn];
int main()
{
while(scanf("%d%d%d",&n,&m1,&m2)!=EOF)
{
memset(g1,,sizeof(g1));
memset(g2,,sizeof(g2));
for(int i=;i<=m1;i++)
{
scanf("%d%d",&u[i],&v[i]);
g1[u[i]][v[i]]=g1[v[i]][u[i]]=;
}
for(int i=;i<=m2;i++)
{
int x,y;
scanf("%d%d",&x,&y);
g2[x][y]=g2[y][x]=;
}
for(int i=;i<=n;i++)a[i]=i;
int ans=,num=;
do
{
int flag1=,flag2=;
for(int i=;i<=m1;i++)
{
int x=a[u[i]],y=a[v[i]];
if(!g1[x][y])flag1=;
if(!g2[x][y])flag2=;
}
ans+=flag2;
num+=flag1;
}while(next_permutation(a+,a+n+));
printf("%d\n",ans/num);
}
return ;
}
//5 3 7
//1 2
//1 3
//1 4
//1 2
//1 3
//1 4
//2 4
//3 4
//4 5
//2 5
暴力版 就是先存下一个同构图temp 在所有的里面找与temp行列式完全相同的有多少个 就是自同构的数量
#include<bits/stdc++.h>
using namespace std;
const int maxn=10,mod=1e9+7;
typedef long long ll;
int g1[maxn][maxn],g2[maxn][maxn],g3[maxn][maxn];
int a[maxn],temp[maxn][maxn];
int main()
{
int n,m1,m2;
while(cin>>n>>m1>>m2)
{
int x,y;
memset(g1,0,sizeof(g1));
memset(g2,0,sizeof(g2));
for(int i=0;i<m1;i++)
{
cin>>x>>y;
g1[x][y]=g1[y][x]=1;
}
for(int i=0;i<m2;i++)
{
cin>>x>>y;
g2[x][y]=g2[y][x]=1;
}
for(int i=1;i<=n;i++)a[i]=i;
do{
int flag=0;
memset(temp,0,sizeof(temp));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(g1[i][j]==1)
{
if(g2[a[i]][a[j]]==0)
{
flag=1;break;
}
else
temp[a[i]][a[j]]=1;
}
}
if(flag)break;
}
if(!flag)break;
}while(next_permutation(a+1,a+n+1));
for(int i=1;i<=n;i++)a[i]=i;
int ans=0,num=0;
do{
int flag=0;
memset(g3,0,sizeof(g3));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(g1[i][j]==1)
{
if(g2[a[i]][a[j]]==0)
{
flag=1;break;
}
else
g3[a[i]][a[j]]=1;
}
}
if(flag)break;
}
if(!flag)
{
ans++;
for(int i=1;i<=n;i++)//{
for(int j=1;j<=n;j++)
if(temp[a[i]][a[j]]!=g3[a[i]][a[j]])flag=1;
if(!flag)num++;
} }while(next_permutation(a+1,a+n+1));
//cout<<ans<<" "<<num<<endl;
cout<<ans/num<<endl;
}
}
J题 区间之外不同数的个数 复制数组 主席树 过的 正解是 离线+树状数组 记录下第一次出现和最后一次出现的位置
代码
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 2e5 + ;
int n,q; int cnt = ; struct Node
{ int l,r,sum; } p[maxn*];
int la[maxn];
int a[maxn]; int root[maxn]; int build(int l,int r)
{ int nc = ++cnt; p[nc].sum = ; p[nc].l = p[nc].r = ; if (l == r) return nc; int m = l + r >> ; p[nc].l = build(l,m); p[nc].r = build(m+,r); return nc; }
int update(int pos,int c,int v,int l,int r)
{
int nc = ++cnt;
p[nc] = p[c];
p[nc].sum += v;
if (l == r) return nc;
int m = l+r>>;
if (m >= pos)
{
p[nc].l = update(pos,p[c].l,v,l,m);
}
else
{
p[nc].r = update(pos,p[c].r,v,m+,r);
}
return nc;
}
int query(int pos,int c,int l,int r)
{
if (l == r) return p[c].sum;
int m = l + r >> ;
if (m >= pos)
{
return p[p[c].r ].sum + query(pos,p[c].l,l,m);
}
else return query(pos,p[c].r,m+,r);
}
int main()
{
while(scanf("%d%d",&n,&q)!=EOF)
{ memset(la,-,sizeof la);
cnt=;
for (int i = ; i <= n; ++i)
{
scanf("%d",a+i);
}
for(int i=n+; i<=*n; i++)
a[i]=a[i-n];
n=n*;
root[] = build(,n);
for (int i = ; i <= n; ++i)
{
int v = a[i];
if (la[v] == -)
{
root[i] = update(i,root[i-],,,n);
}
else
{
int t = update(la[v],root[i-],-,,n);
root[i] = update(i,t,,,n);
}
la[v] = i;
}
while(q--)
{
int x,y;
scanf("%d %d",&x, &y);
printf("%d\n",query(y,root[n/+x],,n));
} }
}
2018牛客网暑期ACM多校训练营(第一场)D图同构,J的更多相关文章
- 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)
2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...
- 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)
Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...
- 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)
链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...
- 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)
链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...
- 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)
题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...
- 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)
题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...
- 2018牛客网暑期ACM多校训练营(第三场) A - PACM Team - [四维01背包][四约束01背包]
题目链接:https://www.nowcoder.com/acm/contest/141/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 2018牛客网暑期ACM多校训练营(第五场) F - take - [数学期望][树状数组]
题目链接:https://www.nowcoder.com/acm/contest/143/F 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]
题目链接:https://www.nowcoder.com/acm/contest/143/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
随机推荐
- [转]为革命保护视力 --- 给 Visual Studio 换颜色
本文转自:http://www.cnblogs.com/stg609/p/3723968.html “为革命,保护视力,预防近视,眼保健操开始......” 这个应该是最老版本的眼保健操了,你听过? ...
- (五)Mybatis总结之一对多、一对一
一对多 业务场景:张三既是java开发师又是大学老师又是LOL代练,张三拥有多个角色. 1.创建实体类UserInfo和RoleInfo package com.qf.mybatisdemo.pojo ...
- Java的知识储备及面试-几个方面
1.Java本身语法基础 https://github.com/Snailclimb/JavaGuide/blob/master/Java%E7%9B%B8%E5%85%B3/Java%E5%9F%B ...
- Java Threads - The volatile keyword
在网上看到很多关于 volatile 关键字的说明和使用问题, 今天引用Java Threads中的解释,感觉全面而详细,可惜是英文的. 这里很清晰的揭示了volatile 本身并不处理java读取数 ...
- 洛谷P2863 [USACO06JAN]牛的舞会The Cow Prom
代码是粘的,庆幸我还能看懂. #include<iostream> #include<cstdio> #include<cmath> #include<alg ...
- C/C++ 标准输入、输出
一.分类 1.标准输入输出 键盘输入,显示器输出.2.文件输入输出 以外存为对象,即硬盘.光盘等.3.串输入输出 对内存中指定空间进行输入输出. 二.c语言中的输入输出 #include <st ...
- java.lang.IllegalArgumentException: Result Maps collection already contains value for com.zhmy.businessapi.mapper.CompanyMapper.BaseResultMap
复制mybatis的mapper.xml文件修改后,忘记将xml中的mapper标签里的namespace更换成对应类了,修改完即可
- SQL表变量与临时表区别 + 非游标临时表遍历
SQL表变量与临时表区别 + 非游标临时表遍历 分类: SQL Server2009-11-27 17:01 1196人阅读 评论(2) 收藏 举报 sqlinsert存储sql servermicr ...
- python 2 encode and decode
https://docs.python.org/2/howto/unicode.html a Unicode string is a sequence of code points, which ar ...
- 用node写个简单的静态服务器
直接上代码吧,我把它命名为 app.js, 只要在该文件所在目录下,控制台运行 node app.js 即可启动一个本地服务器了. /** * 服务器 * Author jervy * Date */ ...