997D Cycles in product
题目大意
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的更多相关文章
- Codeforces 997D - Cycles in product(换根 dp)
Codeforces 题面传送门 & 洛谷题面传送门 一种换根 dp 的做法. 首先碰到这类题目,我们很明显不能真的把图 \(G\) 建出来,因此我们需要观察一下图 \(G\) 有哪些性质.很 ...
- Codeforces997D Cycles in product 【FFT】【树形DP】
题目大意: 给两个树,求环的个数. 题目分析: 出题人摆错题号系列. 通过画图很容易就能想到把新图拆在两个树上,在树上游走成环. 考虑DP状态F,G,T.F表示最终答案,T表示儿子不考虑父亲,G表示父 ...
- Product Management vs. Product Marketing
Posted by Marty Cagan on August 28, 2007 Tags: product management, product marketing, program manage ...
- uva 11059 maximum product(水题)——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK
- [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 ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- vector - vector product
the inner product Givens two vectors \(x,y\in \mathbb{R}^n\), the quantity \(x^\top y\), sometimes c ...
- 1 Maximum Product Subarray_Leetcode
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Leetcode Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- System.Web.HttpRequestValidationException: 从客户端(dbFlag="<soap:Envelope xmlns...")中检测到有潜在危险的 Request.Form 值。
System.Web.HttpRequestValidationException: 从客户端(dbFlag="<soap:Envelope xmlns...")中检测到有潜 ...
- 在Git远程管理项目
新建repository 本地目录下,在命令行里新建一个代码仓库(repository) 里面只有一个README.md 命令如下: touch README.md git init 初 ...
- LeetCode Design TinyURL
原题链接在这里:https://leetcode.com/problems/design-tinyurl/description/ 题目: How would you design a URL sho ...
- 因实现本地浏览器访问nginx修改配置文件后,安装vsftpd失败
解决方法如下(修改dns配置) vi /etc/resolv.conf 在此文件最后加入:nameserver 8.8.8.8 如果没有vi编辑器可用: echo "nameserver 8 ...
- python SQLAlchemy自动生成models文件
1.安装SQLAcodegen pip install sqlacodegen 2.执行 sqlacodegen mysql://root:123456@127.0.0.1:3306/test > ...
- 合并石子(dp)
合并石子 时间限制: 1 Sec 内存限制: 128 MB提交: 7 解决: 7[提交][状态][讨论版][命题人:quanxing] 题目描述 在一个操场上一排地摆放着N堆石子.现要将石子有次序 ...
- Oracle redo与undo
Undo and redo Oracle最重要的两部分数据,undo 与redo,redo(重做信息)是oracle在线(或归档)重做日志文件中记录的信息,可以利用redo重放事务信息,undo(撤销 ...
- 混搭下的C与C++内存操作
源自最近遇到一个的问题,先介绍一下背景.项目中混用了C与C++编程范式,鉴于项目成员背景不一,每个模块的负责人可以自行2选1.同时为了提高效率,C范式的模块被允许使用STL库的部分容器(其实也就仅仅大 ...
- VS2017自定义代码片段, 实现快捷输入
点击VS2017的工具→代码片段管理器, 下图: 语言选择C#, 路径定位到 Visual C#, 然后复制这个路径在电脑中打开 这里以增加 crk 快捷方式输出 Console.ReadKey()来 ...
- web新特性 之 WebSocket
详情参见:你真的了解WebSocket吗? WebSocket系列教程 HTML5新特性之WebSocket WebSocket协议是基于TCP的一种新的协议.WebSocket最初在HT ...