传送门

题目大意

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. python操作rabbitmq、redis

    1.启动rabbimq.mysql 在“”运行“”里输入services.msc,找到rabbimq.mysql启动即可 2.启动redis 管理员进入cmd,进入redis所在目录,执行redis- ...

  2. java向数据库插入时间

    tbUrsMember.setMemberRegisterTime(new Date(System.currentTimeMillis()));

  3. 洛谷 1365 WJMZBMR打osu! / Easy

    题目:https://www.luogu.org/problemnew/show/P1365 大水题.记录一下o的期望长度. 关键是(x+1)^2=x^2+2*x+1. #include<ios ...

  4. PHP 16 个编程法则

    HP是最好的编程语言.对于PHP开发者来说,掌握一些编程法则是十分重要的.而在PHP中,以双下划线(__)开头的方法称为魔术方法,它们扮演着非常重要的角色. 常用的魔术方法包括: -__constru ...

  5. 平台调用之如何利用VS2013 C#调试C++DLL库

    对于托管代码调用非托管DLL文件,已经是非常普遍的事情,下面写一下如何通过托管代码(C#)像调试托管代码一样调试DLL中的代码. 注意:(1)[dll工程和调用dll的exe工程需要在同一个解决方案中 ...

  6. appium 中swipe()方法向左滑动时

    应该在UI Automator Viewer中读取到的例如ImageView [180,600][900,1320],如果要左滑,代码中应该是写为driver.swipe(900,1320,180,6 ...

  7. mysql设计与开发

    架构设计表结构设计索引sql语句1.表结构设计的核心思想是什么?容量评估,性能优化,硬件升级,垂直拆分,水平拆分 2.有个大表为了一个查询(一天就查2次),领导要你建索引(索引空间大小有500G),你 ...

  8. MOS管防止电源反接的原理

    [导读]  一般可以使用在电源的正极串入一个二极管解决,不过,由于二极管有压降,会给电路造成不必要的损耗,尤其是电池供电场合,本来电池电压就3.7V,你就用二极管降了0.6V,使得电池使用时间大减. ...

  9. Linux Restricted Shell Bypass

    Author: @n4ckhcker & @h4d3sw0rm Introduction Hello, so first of all let’s explain what is a rest ...

  10. 使用原生js自定义内置标签

    使用原生js自定义内置标签 效果图 代码 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...