[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)\)( ...
随机推荐
- ActiveMQ(3) ActiveMQ创建(simpleAuthenticationPlugin)安全认证
控制端安全认证: ActiveMQ目录conf下jetty.xml: <bean id="securityLoginService" class="org.ecli ...
- AMD 和 CMD的区别
AMD 是 RequireJS 在推广过程中对模块定义的规范化产出.CMD 是 SeaJS 在推广过程中对模块定义的规范化产出.类似的还有 CommonJS Modules/2.0 规范,是 Brav ...
- lhgdialog的传值问题
一前言 今天就离职了,顺便把还没有记载下来得Js有关知识给记载下来,其实这个是lhgdialog.js中的传值问题.就是弹出框选择数据后加载到父页面上,自己用html做了测试. 二:内容 html代码 ...
- POJ1286 Necklace of Beads
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8263 Accepted: 3452 Description Beads ...
- Django-models class Meta:元类
Django模型之Meta选项详解 Model 元数据就是 "不是一个字段的任何数据" -- 比如排序选项, admin 选项等等. Django模型类的Meta是一个内部类, ...
- linux coredump测试
1 )如何生成 coredump 文件 ? 登陆 LINUX 服务器,任意位置键入 echo "ulimit -c 1024" >> /etc/profile 退出 L ...
- python日志模块笔记
前言 在应用中记录日志是程序开发的重要一环,也是调试的重要工具.但却很容易让人忽略.之前用flask写的一个服务就因为没有处理好日志的问题导致线上的错误难以察觉,修复错误的定位也很困难.最近恰好有时间 ...
- 如何让footer一直在网页底部(不使用绝对定位并且网页不论长度多长)
html: <html> <head> <link rel="stylesheet" href="layout.css" ... ...
- 【SQL】多个表的查询
1.元组变量 SELECT * FROM a AS x, a AS y; 结果是显示自己和自己的笛卡尔乘积. 如果查询中对于某一个关系使用了多次,为了区别他们的属性,需要对关系定义别名,然后用 别名. ...
- python接口自动化11-post传data参数案例【转载】
前言: 前面登录博客园的是传json参数,有些登录不是传json的,如jenkins的登录,本篇以jenkins登录为案例,传data参数. 一.登录jenkins抓包 1.登录jenkins,输入账 ...