题目大意:
  给你一棵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的更多相关文章

  1. [CSAcademy]Cycle Tree

    [CSAcademy]Cycle Tree 题目大意: 定义环树是一张无向连通的简单图,它的生成方式如下: \(2\)个点\(1\)条边的图是环树: 对任意一个环树,加入\(k\)个点\(a_{1\s ...

  2. [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 ...

  3. 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 ...

  4. [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 ...

  5. 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 ...

  6. 【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 ...

  7. Codeforces Gym 100650B Countdown DFS

    Problem B: CountdownTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/conte ...

  8. 《算法4》1.5 - Union-Find 算法解决动态连通性问题,Python实现

    Union-Find 算法(中文称并查集算法)是解决动态连通性(Dynamic Conectivity)问题的一种算法,作者以此为实例,讲述了如何分析和改进算法,本节涉及三个算法实现,分别是Quick ...

  9. [CSAcademy]Find the Tree

    [CSAcademy]Find the Tree 题目大意: 交互题. 有一棵\(n(n\le2000)\)个结点的树,但是你并不知道树的形态.你可以调用\({\rm query}(x,y,z)\)( ...

随机推荐

  1. hdu 3473 (划分树)2

    Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  2. python构建一个项目

    二.实验步骤 2.1 实验准备 我们的实验项目名为 factorial. $ mkdir factorial $ cd factorial/ 2.2 主代码 我们给将要创建的 Python 模块取名为 ...

  3. 通过7zip压缩备份文件bat

    for %%X in (*log20*) do "c:\Program Files\7-Zip\7z.exe" a "backups\%%X.zip" &quo ...

  4. 【转载】How long is “too long” for MySQL Connections to sleep?

    From:http://dba.stackexchange.com/questions/1558/how-long-is-too-long-for-mysql-connections-to-sleep ...

  5. jquery和ajax,json写法的说明

    一: 在ajax中,如果没有用jquery,则如xmlHttpRequest.open("POST", "AjaxServlet", true); (1)如果用 ...

  6. I wrote a JSONHelper extension

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Newtonso ...

  7. python函数对象和闭包

    关于函数对象和闭包 闭包(closure)是函数式编程的重要的语法结构.不同的语言实现闭包的方式不同.Python以函数对象为基础,为闭包这一语法结构提供支持的 (我们在特殊方法与多范式中,已经多次看 ...

  8. HDU1143(3*N的地板铺1*2的砖)

    Tri Tiling Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. IpmiTool常见问题解决办法

    IpmiTool常见问题解决办法 http://blog.csdn.net/c9h8o4/article/details/17138029 关于IPMI的几个问题 http://blog.csdn.n ...

  10. [ Openstack ] Openstack-Mitaka 高可用之 网络服务(Neutron)

    目录 Openstack-Mitaka 高可用之 概述    Openstack-Mitaka 高可用之 环境初始化    Openstack-Mitaka 高可用之 Mariadb-Galera集群 ...