当前位置:网站首页>HDU 4661 message passing (wood DP & amp; Combinatorics)
HDU 4661 message passing (wood DP & amp; Combinatorics)
2022-07-07 01:42:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack
Message Passing
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1187 Accepted Submission(s): 423
Problem Description
There are n people numbered from 1 to n. Each people have a unique message. Some pairs of people can send messages directly to each other, and this relationship forms a structure of a tree. In one turn, exactly one person sends all messages s/he currently has to another person. What is the minimum number of turns needed so that everyone has all the messages? This is not your task. Your task is: count the number of ways that minimizes the number of turns. Two ways are different if there exists some k such that in the k-th turn, the sender or receiver is different in the two ways.
Input
First line, number of test cases, T. Following are T test cases. For each test case, the first line is number of people, n. Following are n-1 lines. Each line contains two numbers.
Sum of all n <= 1000000.
Output
T lines, each line is answer to the corresponding test case. Since the answers may be very large, you should output them modulo 10 9+7.
Sample Input
2
2
1 2
3
1 2
2 3
Sample Output
2
6
Source
zhuyuanchen520 | We have carefully selected several similar problems for you: 501750155013
The question :
n personal , Form a tree relationship . Everyone has a unique message . Everyone can pass their information through the tree . Share with his neighbors , After sharing . The person who is shared has his original information and shared information .
Each share is an operation . Ask how many sharing methods are there for each person to have all their information for the minimum number of times .
Ideas :
easy Out of . In the shortest time . Of course, each node wants to send its own information out once . And receive a message , That is, by the tree 2 times 2*(n-1). Then we can prove . In the shortest time , All delivery methods have one “ Information conversion point ”—— The information of other nodes is first transferred to this node , Then the information is transferred from this node to other nodes .
In fact, what we need is how many kinds of topological orders . Definition dp[u] Express u Below the node . to u How many kinds of topological orders of nodes ,cnt[u] Express u How many children and grandchildren are there ,f[i] = i!(i The factorial ).c[i][j] Represents the number of combinations .
If it has v1,v2,v3 Nodes . Their topological orders are dp[v1],dp[v2],dp[v3] So many .
that dp[u] = c[cnt[u]-1][cnt[v1]] * c[cnt[u]-1-cnt[v1]][cnt[v2]] * c[cnt[u]-1-cnt[v1]-cnt[v2]][cnt[v3]] * dp[v1] * dp[v2] * dp[v3]( Push this by yourself ). After simplification . obtain dp[u] = f[cnt[u]-1] / ( f[cnt[v1]] * f[cnt[v2]] * f[cnt[v3]] ) * dp[v1] * dp[v2] * dp[v3] . We can o(n) The time complexity is calculated with 1 All nodes with root dp value ( So in order to 1 The answer for root is calculated ). And some other auxiliary information . Then traverse down according to the tree structure . Calculate the answers rooted in other nodes respectively . The above is the online thinking . What I want to say is my understanding . Why do you know the number of topological orders of each subtree . You can exit your topological order number . In fact, it's very easy to understand . When the topological order of each subtree is determined . When determining the overall order .
That is to get a length of cnt[u] The topological sequence .
For subtrees i.
There is also a length of cnt[i] The topological sequence , So it's about cnt[u] Look inside cnt[i] A place . Look for other subtrees in the remaining subtrees .
And how to deduce when changing the root . Write first
dp[u]’=dp[u]*(n-sz[v]-1)!*sz[v]!/((n-1)!*dp[v])
dp[v]’=dp[v]*(n-1)!*dp[u]’/((sz[v]-1)!*(n-sz[v])!).
Into the dp[u]’ You can ask for a lot of things . So don't rush to get the final answer when pushing the formula . There is also why the answer number is the square of the topological ordinal .
Because when the information is sent back, it is your topological order . Same as topological ordinal .
Each positive topological order can be combined with an inverse topological order . So there are square species .
See code for details :
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1000010;
#pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long ll;
const ll mod=1e9+7;
ll fac[maxn],dp[maxn],ans;
int cnt,sz[maxn],n;
struct node
{
int v;
node *next;
} ed[maxn<<1],*head[maxn];
void adde(int u,int v)
{
ed[cnt].v=v;
ed[cnt].next=head[u];
head[u]=&ed[cnt++];
}
ll pow_mod(ll x,int k)
{
ll base=x,ret=1;
while(k)
{
if(k&1)
ret=(ret*base)%mod;
base=(base*base)%mod;
k>>=1;
}
return ret;
}
ll ni(ll x){ return pow_mod(x,mod-2); }
void dfs(int fa,int u)
{
ll tp;
dp[u]=tp=sz[u]=1;
for(node *p=head[u];p!=NULL;p=p->next)
{
int v=p->v;
if(v==fa)
continue;
dfs(u,v);
tp=(tp*ni(fac[sz[v]]))%mod;
dp[u]=(dp[u]*dp[v])%mod;
sz[u]+=sz[v];
}
dp[u]=(dp[u]*fac[sz[u]-1]%mod*tp)%mod;
}
void solve(int fa,int u,ll tp)
{
ll tt;
ans=(ans+tp*tp%mod)%mod;
for(node *p=head[u];p!=NULL;p=p->next)
{
int v=p->v;
if(v==fa)
continue;
tt=(tp*fac[n-sz[v]-1]%mod*fac[sz[v]])%mod;
tt=(tt*ni(fac[sz[v]-1])%mod*ni(fac[n-sz[v]]))%mod;
solve(u,v,tt);
}
}
int main()
{
int t,i,u,v,rt;
fac[0]=fac[1]=1;
for(i=2;i<maxn;i++)
fac[i]=(i*fac[i-1])%mod;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
cnt=0,ans=0;
for(i=1;i<=n;i++)
head[i]=NULL;
for(i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
adde(u,v);
adde(v,u);
}
rt=(n+1)/2;
dfs(-1,rt);
solve(-1,rt,dp[rt]);
printf("%I64d\n",ans);
}
return 0;
}
Copyright notice : This article is the original article of the blogger , Blog , Do not reprint without permission .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116894.html Link to the original text :https://javaforall.cn
边栏推荐
- AcWing 904. 虫洞 题解(spfa求负环)
- 从底层结构开始学习FPGA----FIFO IP的定制与测试
- AcWing 346. Solution to the problem of water splashing festival in the corridor (deduction formula, minimum spanning tree)
- ZOJ problem set – 2563 long dominoes [e.g. pressure DP]
- Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
- Let's see how to realize BP neural network in Matlab toolbox
- Wood extraction in Halcon
- 增加 pdf 标题浮窗
- golang 基础 —— 数据类型
- 云呐|工单管理软件,工单管理软件APP
猜你喜欢
制作带照明的DIY焊接排烟器
【C语言进阶篇】指针的8道笔试题
【信号与系统】
AcWing 345. 牛站 题解(floyd的性质、倍增)
云呐|工单管理软件,工单管理软件APP
Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 2)
New job insights ~ leave the old and welcome the new~
盒子拉伸拉扯(左右模式)
爬虫实战(六):爬笔趣阁小说
LeetCode. 剑指offer 62. 圆圈中最后剩下的数
随机推荐
AcWing 346. Solution to the problem of water splashing festival in the corridor (deduction formula, minimum spanning tree)
鼠标右键 自定义
Send template message via wechat official account
Use nodejs to determine which projects are packaged + released
剑指 Offer II 035. 最小时间差-快速排序加数据转换
js如何快速创建一个长度为 n 的数组
Spark TPCDS Data Gen
Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
grep查找进程时,忽略grep进程本身
2022 Google CTF SEGFAULT LABYRINTH wp
【C语言进阶篇】指针的8道笔试题
AcWing 1140. 最短网络 (最小生成树)
一起看看matlab工具箱内部是如何实现BP神经网络的
2022 Google CTF SEGFAULT LABYRINTH wp
AI 从代码中自动生成注释文档
ZOJ problem set – 2563 long dominoes [e.g. pressure DP]
修改px4飞控的系统时间
Google发布安全更新,修复Chrome中已被利用的0 day
初识MySQL
Box stretch and pull (left-right mode)