[CSAcademy]Connected Tree Subgraphs
题目大意:
给你一棵n个结点的树,求有多少种染色方案,使得染色过程中染过色的结点始终连成一块。
思路:
树形DP。
设f[x]表示先放x时,x的子树中的染色方案数,y为x的子结点。
则f[x]=prod{f[y]}*(size[x]-1)!/prod{size[y]}。
现在我们改变f[x]的含义,让其表示先放x时,整棵子树的方案数。
考虑根的转移对答案做出的贡献。
假设我们从x转移到y,那么就要把y从x中剔除,
设剔除y后的f[x]为t,则t=f[x]*size[y]*(size[x]-1-size[y])!/f[y]/(size[x]-1)!。
这是我们要把x子树中的方案数,也就是t算入f[y]中。
f[y]=f[y]*t*(n-1)!/(size[y]-1)!/(n-1-size[y])!。
把t代入,发现f[y]=f[x]*(n-1-size[y])!*size[y]!/(size[y]-1)!/(n-size[y])!。
时间复杂度O(n)。
#include<cstdio>
#include<cctype>
#include<vector>
typedef long long int64;
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=;
const int mod=1e9+;
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
void exgcd(const int &a,const int &b,int &x,int &y) {
if(!b) {
x=;
y=;
return;
}
exgcd(b,a%b,y,x);
y-=a/b*x;
}
inline int inv(const int &x) {
int ret,tmp;
exgcd(x,mod,ret,tmp);
return (ret%mod+mod)%mod;
}
int f[N],fact[N],size[N];
void dfs(const int &x,const int &par) {
f[x]=size[x]=;
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par) continue;
dfs(y,x);
size[x]+=size[y];
f[x]=(int64)f[x]*f[y]%mod*inv(fact[size[y]])%mod;
}
f[x]=(int64)f[x]*fact[size[x]-]%mod;
}
void move(const int &x,const int &par) {
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par) continue;
f[y]=(int64)f[x]*fact[size[]--size[y]]%mod*fact[size[y]]%mod*inv(fact[size[y]-])%mod*inv(fact[size[]-size[y]])%mod;
move(y,x);
}
}
int main() {
const int n=getint();
fact[]=;
for(register int i=;i<n;i++) {
fact[i]=(int64)fact[i-]*i%mod;
}
for(register int i=;i<n;i++) {
add_edge(getint(),getint());
}
int ans=;
dfs(,);
move(,);
for(register int i=;i<=n;i++) {
ans=(ans+f[i])%mod;
}
printf("%d\n",ans);
return ;
}
[CSAcademy]Connected Tree Subgraphs的更多相关文章
- [CSAcademy]Cycle Tree
[CSAcademy]Cycle Tree 题目大意: 定义环树是一张无向连通的简单图,它的生成方式如下: \(2\)个点\(1\)条边的图是环树: 对任意一个环树,加入\(k\)个点\(a_{1\s ...
- [Swift]LeetCode834. 树中距离之和 | Sum of Distances in Tree
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge co ...
- Google Code Jam 2014 Round 1 A:Problem B. Full Binary Tree
Problem A tree is a connected graph with no cycles. A rooted tree is a tree in which one special ver ...
- [LeetCode] 834. Sum of Distances in Tree 树中距离之和
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge co ...
- 834. Sum of Distances in Tree —— weekly contest 84
Sum of Distances in Tree An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges a ...
- 【leetcode】834. Sum of Distances in Tree(图算法)
There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are ...
- Codeforces Gym 100650B Countdown DFS
Problem B: CountdownTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/conte ...
- 《算法4》1.5 - Union-Find 算法解决动态连通性问题,Python实现
Union-Find 算法(中文称并查集算法)是解决动态连通性(Dynamic Conectivity)问题的一种算法,作者以此为实例,讲述了如何分析和改进算法,本节涉及三个算法实现,分别是Quick ...
- [CSAcademy]Find the Tree
[CSAcademy]Find the Tree 题目大意: 交互题. 有一棵\(n(n\le2000)\)个结点的树,但是你并不知道树的形态.你可以调用\({\rm query}(x,y,z)\)( ...
随机推荐
- Bash 实例,第一部分
您可能要问:为什么要学习 Bash 编程?好,以下是几条令人信服的理由: 已经在运行它 如果查看一下,可能会发现:您现在正在运行 bash.因为 bash 是标准 Linux shell,并用于各种目 ...
- 自定义orderBy字母函数
orderedUsers: function () { var arr = this.users; for (var i = 0; i < arr.length - 1; i++) { for ...
- SpringBoot入门学习(一): Idea 创建 SpringBoot 的 HelloWorld
创建项目: 项目结构: 程序启动入口: 正式开始: package com.example.demo; import org.springframework.boot.SpringApplicatio ...
- 动态规划:LCS
先上状态转移方程,还是很容易看明白的 例题是Codevs的1862,这个题不是实现了方程就可以了的,还要完成一个事情那就是计数,数一数到底有多少个最长公共子序列 #include<cstdio& ...
- 【bzoj1597- [Usaco2008 Mar]土地购买】斜率优化
[597][Usaco2008 Mar]土地购买 [题目描述] 有N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000 ...
- #error#错误原因:Cannot find executable for CFBundle 0x8ad60b0 (not loaded)
#error#错误原因:Cannot find executable for CFBundle 0x8ad60b0 </Applications/Xcode.app/Contents/Devel ...
- swift 之嵌套的理解 func chooseStepFunction(backwards: Bool) -> (Int) -> Int
http://blog.csdn.net/lzx_322/article/details/28861199 swift 函数使用前面需要添加 func 有返回值需要使用-> 后面添加返回类型 , ...
- Oracle基础 01 表空间 tablespace
--查看表空间 select * from dba_tablespaces; select * from v$tablespace; select * from dba_data_files; --查 ...
- 带你入门代理模式/SpringAop的运行机制
SpringAop 是spring框架中最重要的一项功能之一,同时也是企业级开发记录事物日志等不可或缺的一部分,如果说你的系统需要记录用户访问接口的操作,那SpringAop是很完美的了,当然,拦截器 ...
- 1.flask视图和URL
1.第一个flask程序 from flask import Flask ''' Flask这个类是项目的核心,以后很多操作都是基于这个类的对象 注册URL等等,都是基于这个类 ''' app = F ...