来自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的更多相关文章

  1. Educational Codeforces Round 17

    Educational Codeforces Round 17 A. k-th divisor 水题,把所有因子找出来排序然后找第\(k\)大 view code //#pragma GCC opti ...

  2. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  3. Educational Codeforces Round 67

    Educational Codeforces Round 67 CF1187B Letters Shop 二分 https://codeforces.com/contest/1187/submissi ...

  4. Educational Codeforces Round 41 967 E. Tufurama (CDQ分治 求 二维点数)

    Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama (CDQ分治 求 二维点数) time limit per test 2 ...

  5. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  6. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  7. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  8. [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 ...

  9. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

随机推荐

  1. 判断mine类型

    var http = require("http"); var fs = require("fs"); var url = require("url& ...

  2. django搭建web (五) views.py

    http请求: HttpRequest http响应: HttpResponse 所在位置:django.http isinstance(request,HttpResponse) True-> ...

  3. vivado License导入方法与资源获取

    前言 以下安装说明基于已经正确安装vivado 笔者操作环境:linux vivado版本:2015.2 vivado License导入方法: 点击菜单栏[Help],选择[Manage Licen ...

  4. Linux环境下发布.net core

    一.安装Linux环境 1. 安装VM虚拟机和操作系统 VM虚拟工具安装的过程详见:http://blog.csdn.net/stpeace/article/details/78598333.直接按照 ...

  5. python之路--day6---文件处理

    一.文件 1.文件就是操作系统提供给应用程序来操作硬盘虚拟概念,用户或应用程序通过操作文件, 可以将自己的数据永久保存下来. 2.操作流程 #1. 打开文件,得到文件句柄并赋值给一个变量--f = o ...

  6. python多目录字符串查找匹配

    1. 需求来自于实际工作: 需要处理一批服务器上运行的redis实例,每个redis实例可能有密码,也可能没有,有密码的,密码配置格式一定是: requirepass XXXXX # XXXX是密码 ...

  7. 可空类型 Nullable<T>

    Nullable<T> 内部实现了显示和隐式转换 显示转换: public static explicit operator T(T? value) { return value.Valu ...

  8. GIT入门笔记(11)- 多种撤销修改场景和对策--实战练习

    1.检查发现目前没有变化$ git statusOn branch masternothing to commit, working tree clean $ cat lsq.txt2222 2.修改 ...

  9. windows计划任务

    前段时间写了一个小工具,实现两个数据库的数据同步. 需求:要求每天的某个时间自动同步数据 功能写好之后,发现了windows一个自带的功能不错,可以实现我的需要,不用写定时器触发了 控制面板-> ...

  10. Stanford依存句法关系解释

    ROOT:要处理文本的语句 IP:简单从句 NP:名词短语 VP:动词短语 PU:断句符,通常是句号.问号.感叹号等标点符号 LCP:方位词短语 PP:介词短语 CP:由'的'构成的表示修饰性关系的短 ...