题目大意:
  给你一棵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. P2765 魔术球问题(网络流24题)

    题目描述 «问题描述: 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为1,2,3,...的球. (1)每次只能在某根柱子的最上面放球. (2)在同一根柱子中,任何2个相邻球的编号之和为完全 ...

  2. python单例与数据库连接池

    单例:专业用来处理连接多的问题(比如连接redis,zookeeper等),全局只有一个对象 单例代码def singleton(cls): instances = {} def _singleton ...

  3. 转:RBAC权限控制

    名词解释: RBAC:Role-Based Access Control,基于角色的访问控制   关键词: RBAC,Java Shiro,Spring Security,   一. RBAC 要解决 ...

  4. 僵尸进程(zombie process)

    僵尸进程(zombie process) http://blog.csdn.net/crfoxzl/article/details/2124718 杀死Linux中的defunct进程(僵尸进程)的方 ...

  5. 用maven创建第一个SpringMVC

    首先创建一个maven项目,然后依次完成如下配置: 在pom.xml加入如下基础配置,利用maven加载我们所需要的jar: <project xmlns="http://maven. ...

  6. boost::bind的简单实现

    前言 在上一篇blog中简单的实现了boost::function,支持带有2个参数的函数/函数指针,函数对象,函数适配器/bind类,以及带有1个参数的成员函数指针. 本文接着来介绍如何实现一个简单 ...

  7. Java反射——java.lang.Class 类简介

    Java的基本思想之一是万事万物即对象,类也是一种对象.但是类是什么对象呢?Java中的类是java.lang.Class的实例化对象,这被成为类类型. //java.lang.Class类中的的主要 ...

  8. 【SQL】使用调用层接口

    只记录C语言相关的,java相关的JDBC和PHP相关的都先跳过. C相关的也只是记录一下,这里面的语句我都不知道如何运行,在我的vs2010里面连头文件都找不到... 我觉得这里只是讲解了一下基本的 ...

  9. 【SQL】宿主语言接口

    一般情况下,SQL语句是嵌套在宿主语言(如C语言)中的.有两种嵌套方式: 1.调用层接口(CLI):提供一些库,库中的函数和方法实现SQL的调用 2.直接嵌套SQL:在代码中嵌套SQL语句,提交给预处 ...

  10. Selenium2+python自动化46-js解决click失效问题【转载】

    前言 有时候元素明明已经找到了,运行也没报错,点击后页面没任何反应.这种问题遇到了,是比较头疼的,因为没任何报错,只是click事件失效了. 本篇用2种方法解决这种诡异的点击事件失效问题 一.遇到的问 ...