HDU 4661 Message Passing 【Tree】
题意:
给一棵树,每一个结点都有一个信息,每一个时刻,某一对相邻的结点之间可以传递信息,那么存在一个最少的时间,使得所有的节点都可以拥有所有的信息。但是,题目不是求最短时间,而是求最短时间的情况下,有多少种传递方式:某一时刻传递信息的双方不一样则认为是不同的传递方式。(表述的不是很清楚,自己看原题了)
容易的出,最短的时间内,当然是每个节点将自己的信息想外传出去一次,并且接受一次信息,也就是树边的2倍【2*(n-1)】。
然后可以证明,在最短时间内,所有的传递方式都有一个“信息转换点”——其他节点的信息首先传递到此节点,然后信息再从这个节点向其他节点传递。
那么总方案数的计算就是可以枚举每个节点,将这个节点作为根节点,然后求当前情况下的方案——先求以当前节点为根的拓扑排序数,然后平方就是当前方案数。
拓扑排序数的计算方法:
c[i] 表示以i为根的子树的节结点(包含结点 i)
f[i] 表示以i为根的子树的拓扑排序数
则:当前结点now的拓扑排序数:
f[now] = f[son1]*f[son2]....f[sonx] * (c[now] - 1)! / (c[son1]! * c[son2]! * ... c[sonx]!)
即:所有子结点的排列数除以每颗子树所有结点的排列数(去重,对于子树的每一种拓扑序),然后乘以所有子树的拓扑序总数。
一遍dfs之后,就可以求出根节点的f值,然后在一边dfs,就可以依次求出所有节点的f值。
转移方法:
根据父节点的f值计算出去掉当前now节点之后的f值,然后以now为根节点,重新计算即可。看代码实现了。
这里用到了逆元,整个程序的速度有点慢。求逆元可以用扩展欧几里得算法,这里用的是费马定理,快速幂:x^(P-2)%P 就是x的逆元。
还有就是树dp,要避免用递归,但是还是用了,会爆栈的,所以就用手工栈了。
手工栈的方法:http://blog.csdn.net/yang_7_46/article/details/9853061
//#pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define N 1000002
#define P 1000000007ll ll fac[N], f[N], ff[N], cc[N], ans;
vector<int> g[N];
int n, c[N]; ll PowerMod(ll a, ll b, ll k) {
ll tmp = a, ret = 1;
while (b) {
if (b & 1) ret = ret * tmp % k;
tmp = tmp * tmp % k;
b >>= 1;
}
return ret;
} void dfs1(int now, int fa) {
int u;
c[now]++;
for (int i=0; i<g[now].size(); i++)
if ((u = g[now][i]) != fa) {
dfs1(u, now);
c[now] += c[u];
} ff[now] = cc[now] = 1;
for (int i=0; i<g[now].size(); i++)
if ((u=g[now][i]) != fa) {
ff[now] = ff[now]*f[u] % P;
cc[now] = cc[now]*fac[c[u]] % P;
}
f[now] = (ff[now]*fac[c[now]-1]%P) * PowerMod(cc[now], P-2, P) % P;
} void dfs2(int now, int fa) {
int u; ll t;
if (now != 1) {
t = f[now]*(n-c[now])%P*cc[now]%P;
t = PowerMod(t, P-2, P);
f[now] = f[fa]*ff[now]%P*fac[c[now]]%P*t%P;
ans = (ans + f[now]*f[now]) % P;
}
for (int i=0; i<g[now].size(); i++)
if ((u = g[now][i]) != fa) dfs2(u, now);
} int main() {
int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p)); fac[0] = 1; for (int i=1; i<N; i++) fac[i] = fac[i-1]*i % P; int T; scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i=0; i<=n; i++) { c[i] = 0; g[i].clear(); }
for (int i=1, x, y; i<n; i++) {
scanf("%d%d", &x, &y);
g[x].push_back(y); g[y].push_back(x);
}
dfs1(1, 0);
ans = f[1] * f[1] % P;
dfs2(1, 0); printf("%I64d\n", ans);
} return 0;
}
HDU 4661 Message Passing 【Tree】的更多相关文章
- hdu 4661 Message Passing(木DP&组合数学)
Message Passing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- HDU 4661 Message Passing ( 树DP + 推公式 )
参考了: http://www.cnblogs.com/zhsl/archive/2013/08/10/3250755.html http://blog.csdn.net/chaobaimingtia ...
- Message Code 【27796】 Failed to connect to server 'hostname';port_ld': 'reason'.
Message Code [27796] Failed to connect to server 'hostname';port_ld': 'reason'.Unable to connect to ...
- HDU 5948 Thickest Burger 【模拟】 (2016ACM/ICPC亚洲区沈阳站)
Thickest Burger Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- HDU 5938 Four Operations 【贪心】(2016年中国大学生程序设计竞赛(杭州))
Four Operations Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))
Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)
Football Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- HDU 3916 Sequence Decomposition 【贪心】
这道题目的题意就是使用题目中所给的Gate 函数,模拟出输入的结果 当然我们分析的时候可以倒着来,就是拿输入去减 每次Gate 函数都会有一个有效范围 这道题目求的就是,找出一种模拟方法,使得最小的有 ...
- UVA 213 Message Decoding 【模拟】
题目链接: https://cn.vjudge.net/problem/UVA-213 https://uva.onlinejudge.org/index.php?option=com_onlinej ...
随机推荐
- Camera实现预览、拍照
1.利用Intent方法实现拍照并保存 在菜单或按钮的选择操作中调用如下代码,开启系统自带Camera APP,并传递一个拍照存储的路径给系统应用程序,具体如下: imgPath = "/s ...
- 初学swift笔记变量的定义(一)
swift变量的定义 1 import Foundation /* 变量的定义 变量的类型是可以不用写的 var a=10 常量的定义 let修饰 */ print(a) let b= print(b ...
- eclipse IDE 扩展pydev
1. 安装PyDev. 运行Eclipse,打开菜单Help->Install New Software.在work with里输入网址:http://pydev.org/updates ,然后 ...
- 一维树状数组(HD1166)
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<string.h> using namespace st ...
- 学习Emacs系列教程
emacs最简单入门,只要10分钟 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3 ...
- rootvg 镜像
具体操作步骤如下 : a) 查看一下当前可用的硬盘: # lspv hdisk0 0002d74f0e69d97a rootvg ...
- spoj 8222 Substrings (后缀自动机)
spoj 8222 Substrings 题意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.求F(1)..F(Length(S)) 解题思路:我们构造S的SAM,那么对于 ...
- Afinal开源框架中FinalActivity的使用
1. 首先将afinal.jar文件复制到项目中的libs文件夹下 2. 让MainActivity不在继承系统的Activity,而是继承FinalActivity public class Mai ...
- 裸的单调队列-poj-2823-Sliding Window
题目链接: http://poj.org/problem?id=2823 题目意思: 给n个数,求连续区间长度为k的最大和最小值. 解题思路: 裸的单调队列不解释,用两个队列保存. 代码: #incl ...
- bounds 和 frame
使用环境: 一个UIView 添加xib View 时需要注意使用 frame and bounds 代码: frame 和 bounds 对比 样式对比 其实说白了就是 frame: 该view在父 ...