Educational Codeforces Round 17F Tree nesting
来自FallDream的博客,未经允许,请勿转载, 谢谢。
给你两棵树,一棵比较大(n<=1000),一棵比较小(m<=12) 问第一棵树中有多少个连通子树和第二棵同构。
答案取膜1e9+7
考虑在大树中随便选个根,然后在小的那棵那里枚举一个根作为大树中深度最小的节点(注意哈希判同构)
然后f[x][y]表示大树的x节点作为小树的y节点的方案数。
如果y是叶子结点,f[x][y]=1
否则要用x的儿子中的一些节点表示y的所有叶子节点。
考虑一个状压dp,g[i][s]表示前i个儿子表示了小树的s集合内的节点的方案数,容易转移。
然后如果y有一些儿子同构,则需要除以个数的阶乘。
复杂度上界是n*m*2^m
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
#define MN 1000
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
unsigned long long ha[MN+];vector<unsigned long long> v[MN+];map<unsigned long long,int> mp,mp2;
int n,m,head[MN+],Head[MN+],fa[MN+],cnt=,f[MN+][],F[<<],size[MN+],son[MN+],q[MN+],ans=,now,P[];
struct edge{int to,next;}e[MN*+];
inline void R(int&x,int y){x+=y;x>=mod?x-=mod:;}
inline void ins(int*H,int f,int t)
{
e[++cnt]=(edge){t,H[f]};H[f]=cnt;
e[++cnt]=(edge){f,H[t]};H[t]=cnt;
} int pow(int x,int k)
{
int sum=;
for(;k;k>>=,x=1LL*x*x%mod)
if(k&) sum=1LL*sum*x%mod;
return sum;
} void GetHa(int x,int f)
{
ha[x]=;size[x]=;v[x].clear();fa[x]=f;
for(int i=Head[x];i;i=e[i].next)
if(e[i].to!=f)
{
GetHa(e[i].to,x);
v[x].push_back(ha[e[i].to]);
}
son[x]=v[x].size();
sort(v[x].begin(),v[x].end());
for(int i=;i<v[x].size();++i) ha[x]=ha[x]*+v[x][i];
ha[x]=ha[x]*+size[x];
} void Solve(int x,int fat)
{
int num=;
for(int i=head[x];i;i=e[i].next)
if(e[i].to!=fat) Solve(e[i].to,x),++num;
for(int i=;i<=m;++i)
{
if(!son[i]){f[x][i]=;continue;}
if(num<son[i]){f[x][i]=;continue;}
for(int k=;k<<<son[i];++k)F[k]=;
F[]=;int top=;
for(int j=Head[i];j;j=e[j].next) if(e[j].to!=fa[i]) q[++top]=e[j].to,++mp2[ha[e[j].to]];
for(int j=head[x];j;j=e[j].next)
if(e[j].to!=fat)
{
for(int s=(<<top)-;~s;--s)
for(int k=;k<=top;++k) if(f[e[j].to][q[k]])
if(!(s&(<<k-))) R(F[s|(<<k-)],1LL*F[s]*f[e[j].to][q[k]]%mod);
}
f[x][i]=F[(<<top)-];
for(int j=;j<=top;++j)
if(mp2[ha[q[j]]]) f[x][i]=1LL*f[x][i]*pow(P[mp2[ha[q[j]]]],mod-)%mod,mp2[ha[q[j]]]=;
}
R(ans,f[x][now]);
} int main()
{
n=read();P[]=;
for(int i=;i<=;++i) P[i]=1LL*P[i-]*i%mod;
for(int i=;i<n;++i) ins(head,read(),read());m=read();
for(int i=;i<m;++i) ins(Head,read(),read());
for(int i=;i<=m;++i)
{
GetHa(i,);
if(mp[ha[i]]) continue;
memset(f,,sizeof(f));mp[ha[i]]=;
now=i;Solve(,);
}
printf("%d\n",ans);
return ;
}
Educational Codeforces Round 17F Tree nesting的更多相关文章
- Educational Codeforces Round 17
Educational Codeforces Round 17 A. k-th divisor 水题,把所有因子找出来排序然后找第\(k\)大 view code //#pragma GCC opti ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Educational Codeforces Round 67
Educational Codeforces Round 67 CF1187B Letters Shop 二分 https://codeforces.com/contest/1187/submissi ...
- Educational Codeforces Round 41 967 E. Tufurama (CDQ分治 求 二维点数)
Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama (CDQ分治 求 二维点数) time limit per test 2 ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
随机推荐
- pickle使用及案例
一.字典格式数据源写入数据库文件 #!/usr/bin/env python # -*- coding:utf-8 -*- import pickle accounts ={1000:'alex', ...
- PM2使用心得
PM2是node进程管理工具,可以利用它来简化很多node应用管理的繁琐任务,如性能监控.自动重启.负载均衡等,而且使用非常简单. 安装 npm install -g pm2 常用命令 $ npm i ...
- js中多维数组转一维
法一:使用数组map()方法,对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组. var arr = [1,[2,[[3,4],5],6]]; function unid(arr){ v ...
- Three.js three.js Uncaught TypeError: Cannot read property 'getExtension' of null
在调试Three.js执行加载幕布的时候,突然爆出这个错误three.js Uncaught TypeError: Cannot read property 'getExtension' of nul ...
- PHP分页初探 一个最简单的PHP分页代码的简单实现
PHP分页代码在各种程序开发中都是必须要用到的,在网站开发中更是必选的一项. 要想写出分页代码,首先你要理解SQL查询语句:select * from goods limit 2,7.PHP分页代码核 ...
- C# 使用 GDI+ 给图片添加文字,并使文字自适应矩形区域
需求 需求是要做一个编辑文字的页面.用户在网页端写文字,文字区域是个矩形框,用户可以通过下方的拖动条调节文字大小. 如下图: 提交数据的时候前端传文字区域的左上角和右下角定位给后台.因为前端的字体大小 ...
- SpringCloud的DataRest(三)
一.进阶配置定义 二.定制显示内容和控制方法使用 1.控制显示的内容 如果想让输出显示的数据始终按我们定义的格式显示: 配置之后返回的JSON数据会按照ListApp定义的数据格式进行输出 2.屏蔽自 ...
- hadoop原理
MapReduce工作原理图文详解 前言: 前段时间我们云计算团队一起学习了hadoop相关的知识,大家都积极地做了.学了很多东西,收获颇丰.可是开学后,大家都忙各自的事情,云计算方面的动静都不太 ...
- IDE-Ecplise-代码注释 模版 编码规范 配色
说明: 代码注释主要用于方便代码后期维护,编码规范,增加代码阅读性和维护性.因网上看到的很多博客中片段局多,故整理后重写一篇,方便交流学习. 先看下加过注释模版后的效果. 如上图所示,创建类,方法和继 ...
- Ecplise 配置本地 https 测试
今天做项目,需要关联Office 365.为了实现Office365的用户邮件信息与项目的实时同步,需要建立webhook订阅. Office 365 API 连接 https://graph.mi ...