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 ...
随机推荐
- php中除法取整的方法(round,ceil,floor)
PHP中遇到需要将除法所得结果取整的情况时,就需要用到以下方法: 1. round:四舍五入 round() 函数对浮点数进行四舍五入. 语法:round(x, prec) 参数 描述 x 可选.规定 ...
- Asp.Net中调用存储过程并返回输出参数
/// <summary> /// 调用存储过程返回参数 /// </summary> /// <param name="orderId">&l ...
- Java线程-线程的基本状态
问题:线程有哪些基本状态?这些状态是如何定义的? 新建(new):新创建了一个线程对象. 可运行(runnable):线程对象创建后,其他线程(比如main线程)调用了该对象的start()方法.该状 ...
- python strip() 函数探究
strip()方法语法:str.strip([chars]); 声明:str为字符串,rm为要删除的字符序列 str.strip(rm) 删除字符串中开头.结尾处,位于rm删除序列的字符 eg1: # ...
- C/C++ new/delete []、内存泄漏、动态数组
一.概念 new/delete是用于动态分配和撤销内存的运算符.new/delete是c++里才有的,c中是用malloc和free,c++虽然也可以用,但是不建议用.当我们使用关键字new在堆上动态 ...
- 几个net命令
A.显示当前工作组服务器列表 net view,当不带选项使用本命令时,它就会显示当前域或网络上的计算机上的列表. 比如:查看这个IP上的共享资源,就可以 C:\\>net view 192 ...
- 关于TreeView控件的TreeNodeCheckChanged事件无法回发处理
1.在后台设置属性 TreeView1.Attributes.Add("onclick", "postBackByObject()"); 2.在前台页面中间添加 ...
- rtim() 函数说明
rtim() 函数 string rtrim ( string $str [, string $character_mask ] ) 该函数删除 str 末端的空白字符(或者其他字符)并返回. 不使用 ...
- new Buffer 生成二进制数据
node编辑环境下: > new Buffer("admin")<Buffer 61 64 6d 69 6e> 通过post请求,服务端接收到是流数据,必须把流数 ...
- CPU怎么计算1+1----CPU计算的电路基础
从<十进制和二进制的运算---我所理解到的人类的运算的本质>这里我们知道,人类进行运算的本质是查表,并且我们存储的表是有限的.那么计算机是怎进行四则运算的呢,也是查表吗,肯定不是,今天,我 ...