[Educational Round 17][Codeforces 762F. Tree nesting]
题目连接:762F - Tree nesting
题目大意:给出两个树\(S,T\),问\(S\)中有多少连通子图与\(T\)同构。\(|S|\leq 1000,|T|\leq 12\)
题解:考虑树的最小表示法(有关知识可戳https://www.byvoid.com/zhs/blog/directed-tree-bracket-sequence),求出\(T\)以不同点为根时所有的子树状态
开始对树\(S\)进行\(DFS\),求出每个点的状态为\(t\)时的方案数,由于\(t\)还是由\(n\)个数字(括号序列)合并起来的,而且\(n\)不会太大,所以可以用二进制DP求解
对当前点求解时,只需遍历其儿子,将儿子的解并入当前的状态即可。由于一个点可能有若干个形状相同的子树,所以要考虑去重,具体实现见代码
#include<bits/stdc++.h>
using namespace std;
#define N 1001
#define M 1<<12
#define MOD 1000000007
int len(int x){return -__builtin_clz(x);}
int Union(int x,int y){return (x<<len(y))|y;}
struct Tree
{
int n,ans;
int f[M][];
vector<int>d[N];
map<int,int>num[N];
map<int,vector<int> >mp;
void read()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
d[u].push_back(v);
d[v].push_back(u);
}
}
int dfs(int cur,int pre)
{
int res=;
vector<int>tmp;
//tmp用来记录子树的状态
for(auto nxt:d[cur])if(nxt!=pre)
tmp.push_back(dfs(nxt,cur));
sort(tmp.begin(),tmp.end());
for(auto x:tmp)res=Union(res,x);
res<<=;
//res表示当前节点的最小表示
if(!mp.count(res))mp[res]=tmp;
//将当前点对应的子树们也记录下来
return res;
}
void getID()
{
for(int i=;i<=n;i++)
dfs(i,);
//枚举以所有的点为根的情况
}
void DP(int cur,int pre,const Tree &T)
{
for(auto nxt:d[cur])if(nxt!=pre)DP(nxt,cur,T);
for(const auto &pi:T.mp)//枚举T中的所有状态
{
auto &types=pi.second;
int id=pi.first,n=types.size(),now=,lst=;
for(int i=;i<(<<n);i++)f[i][]=f[i][]=;
f[][]=;
//id为当前枚举到的状态,n为当前状态拥有的子树数目,用滚动数组实现儿子们的合并
for(auto nxt:d[cur])if(nxt!=pre)
{
now^=,lst^=;
for(int i=;i<(<<n);i++)
f[i][now]=f[i][lst];
for(int i=;i<n;i++)
if(num[nxt][types[i]])//num[i][j]表示点i的状态为j时方案有多少个
for(int j=(<<n)-;j>=;j--)
if(f[j][lst] && !((<<i)&j) && !(i && types[i]==types[i-] && !((<<(i-))&j)))
// (i && types[i]==types[i-1] && !((1<<(i-1))&j)代表的是
//当前枚举的子树和前一个子树同构 ,且前一个子树的状态未记录
(f[(<<i)|j][now]+=1ll*f[j][lst]*num[nxt][types[i]]%MOD)%=MOD;
}
if(f[(<<n)-][now])
{
num[cur][id]=f[(<<n)-][now];//集齐了所有子树即为对应num的答案
if(len(id)==*T.n)(ans+=num[cur][id])%=MOD;//id的二进制长度为2m则说明一定是根节点的状态,加入答案
}
}
}
}S,T;
int main()
{
S.read();
T.read();
T.getID();
S.DP(,,T);
printf("%d\n",S.ans);
}
[Educational Round 17][Codeforces 762F. Tree nesting]的更多相关文章
- [Codeforces]762F - Tree nesting
题目大意:给出一棵n个点的树和一棵m个点的树,问第一棵树有多少个连通子树与第二棵树同构.(n<=1000,m<=12) 做法:先找出第二棵树的重心(可能为边),以这个重心为根,可以避免重复 ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
- [Educational Round 5][Codeforces 616F. Expensive Strings]
这题调得我心疲力竭...Educational Round 5就过一段时间再发了_(:з」∠)_ 先后找了三份AC代码对拍,结果有两份都会在某些数据上出点问题...这场的数据有点水啊_(:з」∠)_[ ...
- [Educational Round 3][Codeforces 609F. Frogs and mosquitoes]
这题拖了快一周_(:з」∠)_就把这货单独拿出来溜溜吧~ 本文归属:Educational Codeforces Round 3 题目链接:609F - Frogs and mosquitoes 题目 ...
- [Educational Round 13][Codeforces 678F. Lena and Queries]
题目连接:678F - Lena and Queries 题目大意:要求对一个点集实现二维点对的插入,删除,以及询问\(q\):求\(max(x\cdot q+y)\) 题解:对每个点集内的点\(P( ...
- [Educational Round 10][Codeforces 652F. Ants on a Circle]
题目连接:652F - Ants on a Circle 题目大意:\(n\)个蚂蚁在一个大小为\(m\)的圆上,每个蚂蚁有他的初始位置及初始面向,每个单位时间蚂蚁会朝着当前面向移动一个单位长度,在遇 ...
- [Educational Round 59][Codeforces 1107G. Vasya and Maximum Profit]
咸鱼了好久...出来冒个泡_(:з」∠)_ 题目连接:1107G - Vasya and Maximum Profit 题目大意:给出\(n,a\)以及长度为\(n\)的数组\(c_i\)和长度为\( ...
- 【Henu ACM Round#17 E】Tree Construction
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 做这题之前先要知道二叉排序树的一个性质. 就是它的中序遍历的结果就是这个数组升序排序. (且每个节点的左边的节点都是比这个节点的值小 ...
- Educational Codeforces Round 17
Educational Codeforces Round 17 A. k-th divisor 水题,把所有因子找出来排序然后找第\(k\)大 view code //#pragma GCC opti ...
随机推荐
- 区块链之智能合约 solidity踩坑 --上篇
概述 最近在写合约时遇到一些坑,做一下总结: 介绍主要分一下三个方面: 对区块链的简单描述 结合业务场景,编写简单智能合约,时遇到的坑(上篇) assembly 的使用说明(下篇) 正文 进入正题之前 ...
- Spring Cloud 2-Hystrix DashBoard仪表盘(五)
Spring Cloud Hystrix DashBoard 1.监控系统配置 pom.xml application.yml Application.java 2.被监控服务配置 pom.xml ...
- NB-IoT有三种部署方式及特点【转】
转自:http://blog.sina.com.cn/s/blog_13ddf053f0102wcbz.html NB-IoT有三种运营模式,一种是独立的在运营商的网络外面重做:第二种是在LTE的保护 ...
- windows 环境下切换 python2 与 pythone3 以及常用命令
windows 环境下切换运行时的 python2 与 pythone3 当需要 python2 时执行:py -2 当需要 python3 时执行:py -3 windows下通过cmd切换pyth ...
- CSS --记录
CSS3与文字渐变光影流动动画效果实现 by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/word ...
- Lesson 1-2
1.5 模块 模块可视为扩展,通过将其导入可以扩展python的功能.python中自带有一组模块,也称为“标准库”. 1.5.1 模块的导入:import + 模块名称 • 使用关键字import导 ...
- 公设基础equals
1# 覆盖equals方法的通用约定 1.自反性(reflexive) 自己跟自己的比较必须返回true 2.对称性(symmetric) x=y那么y=z 3.传递性(transitive) x= ...
- memcpy一种实现方法
#include<stdio.h> #include<stdlib.h> void* memncpy(void* dest, const void* src, int coun ...
- TPshop之邮箱注册配置教程--附加常见问题集合
准备:企业邮箱(开启POP/SMTP功能) 一.步骤教程: 1.登录企业邮箱(QQ邮箱示例) QQ邮箱 POP3:pop.qq.com SMTP:smtp.qq.com SMTP端口号:25 邮箱 ...
- 烽火2640路由器命令行手册-11-IP语音配置命令
IP语音配置命令 目 录 第1章 配置拨号对命令... 1 1.1 配置拨号对命令... 1 1.1.1 dial-peer voice. 1 1.1.2 application. 2 1.1.3 ...