传送门

题目大意

https://www.luogu.org/problemnew/show/CF997D

分析

我们发现两棵树互不相关

于是我们可以分别求出两棵树的信息

我们点分,人啊按后设f[i][x]为从根出发走i步到x中间不经过根的方案数,g[i][x]为可以经过根的方案数

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define add(x,y) x=(x+y)%mod
const int mod = ;
int c[][],m;
struct tree {
int n,ans[],f[][],g[][];
int root,sum,siz[],w[],a[],tot;
bool vis[];
vector<int>v[];
inline void getrt(int x,int fa){
siz[x]=;
w[x]=;
for(int i=;i<v[x].size();i++)
if(v[x][i]!=fa&&!vis[v[x][i]]){
getrt(v[x][i],x);
siz[x]+=siz[v[x][i]];
w[x]=max(w[x],siz[v[x][i]]);
}
w[x]=max(w[x],sum-siz[x]);
if(!root||w[x]<w[root])root=x;
}
inline void dfs(int x,int fa){
a[++tot]=x;
siz[x]=;
for(int i=;i<v[x].size();i++)
if(v[x][i]!=fa&&!vis[v[x][i]]){
dfs(v[x][i],x);
siz[x]+=siz[v[x][i]];
}
}
inline void work(int x){
tot=;
dfs(x,);
int i,j,k;
memset(f,,sizeof(f));
memset(g,,sizeof(g));
f[][x]=g[][x]=;
for(i=;i<=m;i++)
for(j=;j<=tot;j++){
for(k=;k<v[a[j]].size();k++){
if(vis[v[a[j]][k]])continue;
if(a[j]!=x)add(f[i][a[j]],f[i-][v[a[j]][k]]);
add(g[i][a[j]],g[i-][v[a[j]][k]]);
}
}
for(i=;i<=tot;i++){
if(a[i]==x){
for(j=;j<=m;j++)add(ans[j],g[j][a[i]]);
}else {
for(j=;j<=m;j++)
for(k=;k+j<=m;k++)
add(ans[k+j],(long long)f[j][a[i]]*g[k][a[i]]%mod);
}
}
vis[x]=;
for(i=;i<v[x].size();i++)
if(!vis[v[x][i]]){
root=;
sum=siz[v[x][i]];
getrt(v[x][i],x);
work(root);
}
}
inline void solve(){
sum=n;
root=;
memset(vis,,sizeof(vis));
memset(ans,,sizeof(ans));
getrt(,);
work(root);
}
};
tree t1,t2;
int main(){
int i,j,k;
scanf("%d%d%d",&t1.n,&t2.n,&m);
c[][]=;
for(i=;i<=m;i++)c[i][i]=c[i][]=;
for(i=;i<=m;i++)
for(j=;j<i;j++)c[i][j]=(c[i-][j]+c[i-][j-])%mod;
for(i=;i<=t1.n;i++)t1.v[i].clear();
for(i=;i<=t2.n;i++)t2.v[i].clear();
for(i=;i<t1.n;i++){
int x,y;
scanf("%d%d",&x,&y);
t1.v[x].push_back(y);
t1.v[y].push_back(x);
}
for(i=;i<t2.n;i++){
int x,y;
scanf("%d%d",&x,&y);
t2.v[x].push_back(y);
t2.v[y].push_back(x);
}
t1.solve(),t2.solve();
int Ans=;
for(i=;i<=m;i++)
Ans=(Ans+(long long)t1.ans[i]*t2.ans[m-i]%mod*c[m][i]%mod)%mod;
cout<<Ans;
return ;
}

997D Cycles in product的更多相关文章

  1. Codeforces 997D - Cycles in product(换根 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 一种换根 dp 的做法. 首先碰到这类题目,我们很明显不能真的把图 \(G\) 建出来,因此我们需要观察一下图 \(G\) 有哪些性质.很 ...

  2. Codeforces997D Cycles in product 【FFT】【树形DP】

    题目大意: 给两个树,求环的个数. 题目分析: 出题人摆错题号系列. 通过画图很容易就能想到把新图拆在两个树上,在树上游走成环. 考虑DP状态F,G,T.F表示最终答案,T表示儿子不考虑父亲,G表示父 ...

  3. Product Management vs. Product Marketing

    Posted by Marty Cagan on August 28, 2007 Tags: product management, product marketing, program manage ...

  4. uva 11059 maximum product(水题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK

  5. [LeetCode] Product of Array Except Self 除本身之外的数组之积

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  6. [LeetCode] Maximum Product Subarray 求最大子数组乘积

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. vector - vector product

    the inner product Givens two vectors \(x,y\in \mathbb{R}^n\), the quantity \(x^\top y\), sometimes c ...

  8. 1 Maximum Product Subarray_Leetcode

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  9. Leetcode Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

随机推荐

  1. web 模板引擎

    baiduTemplate:  http://baidufe.github.io/BaiduTemplate/ artTemplate: https://github.com/aui/artTempl ...

  2. 自动工作负载库理论与操作(Automatic Workload Repository,AWR)

    AWR的由来:    10g之前的oracle:用户的连接将产生会话,当前会话记录保存在v$session中:处于等待状态的会话会被复制一份放在v$session_wait中.当该连接 断开后,其原来 ...

  3. dpkg安装工具

    dpkg --info "软件包名" --列出软件包解包后的包名称. dpkg -l --列出当前系统中所有的包.可以和参数less一起使用在分屏查看. (类似于rpm -qa) ...

  4. php-fpm.conf 配置文件详解

    php-fpm.conf  配置文件详解 [global] pid = run/php-fpm.pid error_log = log/php-fpm.log log_level = notice # ...

  5. 你知道PORT吗?

    在TCP协议中,有端口(PORT)的概念,很多人都不知道端口到底是什么.之前介绍过物理地址,也就是网卡地址,做个不恰当的比喻,物理地址(MAC)地址,相当于身份证(唯一),家庭地址是几幢几单元相当于I ...

  6. java代码从键盘输入n的值,计算1+1/2+1/3+...+1/n的值,,

    总结:谢谢陈勇老师.很棒的指导.超有爱. 总是不思考++++如内存的分析.堆和栈.堆内存里对象,字符串,栈里基本数据类型 来龙去脉,属性方法的调用,都不是很理解.... package com.c2; ...

  7. MongoDB 4.X搭建

    一.MongoDB4.X搭建 1.下载mongdb安装包,在官网上找到对应的版本,我的是centos7 找到上面的连接,通过命令行: 2.将下载的mongodb-linux-x86_64-4.0.0. ...

  8. Spring缓存源码剖析:(一)工具选择

    从本篇开始对Spring 4.3.6版本中Cache部分做一次深度剖析.剖析过程中会对其中使用到的设计模式以及原则进行分析.相信对设计内功修炼必定大有好处. 一.环境及工具 IntelliJ IDEA ...

  9. PHP通过引用传递参数

    <?php function add_some_extra(&$string) // 引入变量,使用同一个存储地址 { $string .= 'and something extra.' ...

  10. iOS平台下闪退原因汇总(一):"Ran out of trampolines of type 0/1/2" 运行时间错误

    "Ran out of trampolines of type 0/1/2" 运行时间错误通常出现在使用大量递归泛型时.要看到这个错误需要连接着设备直接将项目build到设备里运行 ...