当前位置:网站首页>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 3Sample Output
2
6Source
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 1142. Busy urban problem solving (minimum spanning tree)
- Yunna | work order management software, work order management software app
- Wood extraction in Halcon
- 【信号与系统】
- C language instance_ two
- JS ES5也可以创建常量?
- Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- Transplant DAC chip mcp4725 to nuc980
- AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
猜你喜欢

Gin 入门实战

New job insights ~ leave the old and welcome the new~

AI automatically generates annotation documents from code

The cradle of eternity

AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)

Typical problems of subnet division and super network construction

dvajs的基础介绍及使用

Wood extraction in Halcon

Appium foundation - appium inspector positioning tool (I)

子网划分、构造超网 典型题
随机推荐
制作带照明的DIY焊接排烟器
C language - array
C language instance_ two
Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
WCF Foundation
Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 1)
设置Wordpress伪静态连接(无宝塔)
ZOJ Problem Set – 2563 Long Dominoes 【如压力dp】
2022 Google CTF SEGFAULT LABYRINTH wp
According to the analysis of the Internet industry in 2022, how to choose a suitable position?
mongodb查看表是否导入成功
各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
hdu 4661 Message Passing(木DP&amp;组合数学)
Appium automation test foundation uiautomatorviewer positioning tool
Comparison of picture beds of free white whoring
json学习初体验–第三者jar包实现bean、List、map创json格式
C language instance_ three
Share a general compilation method of so dynamic library
对C语言数组的再认识
IDEA常用的快捷键