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 ...
随机推荐
- 静态关键字static用法。
static的特点:1,static是一个修饰符,用于修饰成员.2,static修饰的成员被所有的对象所共享.3,static优先于对象存在,因为static的成员随着类的加载就已经存在了. 4,st ...
- 玩转Leveldb原理及源码--拙见1
可以说是不知天高地厚.. 可以说是班门弄斧.. 但是,我今天还就这样走了,我喜欢!!!!!! 注:后续文章,限于篇幅,不懂名词都有 紫色+下划线 超链接,有兴趣,可以查阅: 网上关于Leveldb 的 ...
- itchat 微信的使用
#coding=utf8 import itchat # 自动回复 # 封装好的装饰器,当接收到的消息是Text,即文字消息 @itchat.msg_register('Text') def text ...
- javascript递归函数
递归函数:是指函数直接或间接调用函数本身,则称该函数为递归函数. 这句话理解起来并不难,从概念上出发,给出以下的例子: function foo(){ console.log("函数 foo ...
- ORA-00379 缓冲池 DEFAULT 中无法提供 32K 块大小的空闲缓冲区
(一)问题 今天在使用Pl/sql developer查看表空间大小的时候,报错误:ORA-00379 缓冲池 DEFAULT 中无法提供 32K 块大小的空闲缓冲区,具体如下图: SQL> s ...
- 新概念英语(1-37)Making a bookcase
What is Susan's favourite color ? A:You're working hard, Georage. What are you doing? B:I am making ...
- linux下Tab及shell 补全python
Python自动补全 Python自动补全有vim编辑下和python交互模式下,下面分别介绍如何在这2种情况下实现Tab键自动补全. vim python自动补全插件:pydiction 可以实现下 ...
- Vue框架
Vue框架 环境: windows python3.6.2 Vue的cdn: <script src="https://cdn.jsdelivr.net/npm/vue"&g ...
- Struts(二十八):自定义拦截器
Struts2拦截器 拦截器(Interceptor)是Struts2的核心部分. Struts2很多功能都是构建在拦截器基础之上,比如:文件上传.国际化.数据类型转化.数据校验等. Struts2拦 ...
- scrapy爬取豆瓣电影top250
# -*- coding: utf-8 -*- # scrapy爬取豆瓣电影top250 import scrapy from douban.items import DoubanItem class ...