传送门

题目大意

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. 【SQL】分组数据,过滤分组-group by , having

    学习笔记,原文来自http://blog.csdn.net/robinjwong/article/details/24845125 创建分组 - GROUP BY 分组是在SELECT语句的GROUP ...

  2. WCF OpenTimeout, CloseTimeout, SendTimeout, ReceiveTimeout

    1.OpenTimeout 客户端与服务端建立连接时,如果超过指定时间都还没完成,就引发TimeoutException. 在TCP通讯中,服务器必须首先准备好侦听端口并在该端口上侦听(Listen) ...

  3. 洛谷 P1292 倒酒

    题目描述 Winy是一家酒吧的老板,他的酒吧提供两种体积的啤酒,a ml和b ml,分别使用容积为a ml和b ml的酒杯来装载. 酒吧的生意并不好.Winy发现酒鬼们都非常穷.有时,他们会因为负担不 ...

  4. rpm -Uvh 升级时的陷阱

    问题现象 用rpm -Uvh升级后,原先的一个软链接被删除了,而采用先rpm -e 卸载rpm包,再rpm -ivh 安装包的方法,这个软链接还在.这个软链接是在rpm包安装的时候建立,也只有在rpm ...

  5. oracle fn project 开源faas 框架

    1. 介绍 Fn is an event-driven, open source, functions-as-a-service compute platform that you can run a ...

  6. log框架集成

    使用slf4j,slf4j相当于一个接口,我们面对接口编程,方便地集成其他的日志框架,我们按照slf4j的标准,日志就会相应地打入日志系统中(log4j 使用slf4j要有两个包1,他本身的api,2 ...

  7. bzoj 3566 [SHOI2014]概率充电器——树型

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3566 一眼看上去高斯消元.n^3不行. 竟然直接去看了TJ.发现树型dp.一下想到了自己还没 ...

  8. 【android】Socket简单用法

    Socket通常也称做”套接字“,用于描述IP地址和端口,废话不多说,它就是网络通信过程中端点的抽象表示.值得一提的是,Java在包java.net中提供了两个类Socket和ServerSocket ...

  9. laravel中session的过期时间

    在项目开发的过程中,前后端分离 需要用session保存用户的登陆信息 这就涉及到session的有效期了 session又分为php中的session有效期和laravel中的session的有效期 ...

  10. RHEL6.2配置从零开始

    RHEL6.2最小化安装并配置,持续更新中... 1.RHEL6.2最小化安装 RHEL6.2默认安装许多用不到的软件,不仅浪费空间.增大系统开销,还会显得凌乱.所以选择最小化安装.  注意:安装步骤 ...