传送门

题目大意

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. Info.plist字段列表详解

    常用字段: 1.获取版本信息: NSDictionary*infoDic = [[NSBundle mainBundle] infoDictionary]; NSString *localVersio ...

  2. 客户端如何调用WebService并且POST数据

    直接上代码 using System; using System.Collections.Generic; using System.Linq; using System.Text;using Sys ...

  3. tensorflow dropout

    我们都知道dropout对于防止过拟合效果不错dropout一般用在全连接的部分,卷积部分不会用到dropout,输出曾也不会使用dropout,适用范围[输入,输出)1.tf.nn.dropout( ...

  4. (转)Android高性能编程(1)--基础篇

    关于专题     本专题将深入研究Android的高性能编程方面,其中涉及到的内容会有Android内存优化,算法优化,Android的界面优化,Android指令级优化,以及Android应用内存占 ...

  5. 64位linux下rpm安装mysql的5.5.55版本

    昨天同事开了个阿里云环境,让我帮他安装mysql数据库,本想着很简单的一件事,结果还是折腾了一番.坑很多,一路趟过,一个接一个,只能硬着头皮冲. 首先是下载压缩包,因为采用了rpm安装方式,所以下载的 ...

  6. python3 之 linux命令实现

    os.mkdir(path[, mode]) 以数字mode的mode创建一个名为path的文件夹.默认的 mode 是 0777 (八进制) # 创建多级目录 mkdir -p dir1/dir2 ...

  7. win10下默认使用福昕打开PDF

    win10为了推他的edge浏览器, 将默认的pdf打开设置为了edge浏览器, 非常令人反感, 做浏览器就好好做浏览器, 为什么要默认打开pdf? 而且修改默认为福昕后, 下次打开pdf文件, 他又 ...

  8. C# 通用类型转换方法

    在程序开发过程中经常需要进行数据的类型转换,而且如果一个字段的类型改成另一个类型时,所有相关的类型转换的地方都要跟着修改,不但造成了很多重复转换的代码而且修改字段类型时额外修改相关转换代码的工作量也很 ...

  9. python使用pika链接rabbitmq Connection reset by peer 解决办法

    记录一下, 最近在用机器学习打算做一个Rest API, 数据存入mongo,任务采用消息队列,rabbitmq 由于引擎采用python编写,所以WEB也直接打算用python编写了,比较省事. W ...

  10. [机器学习]numpy broadcast shape 机制

    最近在做机器学习的时候,对未知对webshell检测,发现代码提示:ValueError: operands could not be broadcast together with shapes ( ...