HDU-4661 Message Passing 树形DP,排列组合

2022-10-29,,,,

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4661

  题意:有n个人呈树状结构,每个人知道一个独特的消息。每次可以让一个人将他所知的所有消息告诉和他相邻的人。求所有人都知道所有消息花时花的步数最少的所有方案数。

  首先需要满足的是最小的步数,所以我们一定是先把所有消息先传到一个人手中才是最优的,然后再从这个人传回去,也就是每条边走两次。我们只需要考虑单向传到某个人的方案数cnt,因为再传回去也是cnt。那么我们可以枚举每个点为收集点,把所有的和加起来就是答案。这里就是一个树形DP的问题,转移的时候是一个组合问题:记录f[u],cnt[u],f[u]为以u为根的子树的拓扑排序数,cnt[u]为以u为子树的节点个数。这里,涉及到如何合并两个子树,比如u的两个子树v1,v2,那么合并后的拓扑排序数为:f[u]=f[son1]*f[son2]*C(cnt[son1]+cnt[son2],cnt[son1]),也就是:

  这里只求出了根节点的方案,还要再做一次搜索,把其它点的方案求出了,根据上面那个公式推一下就可以了。。。

 //STATUS:C++_AC_3062MS_49796KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const LL MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End LL fac[N],rev[N],f[N],cnt[N];
int T,n;
LL ans;
vector<int> q[N]; void exgcd(LL a,LL b,LL &d,LL &x,LL &y)
{
if(!b){d=a;x=;y=;}
else {exgcd(b,a%b,d,y,x);y-=x*(a/b);}
} LL inv(LL a,LL n)
{
LL d,x,y;
exgcd(a,n,d,x,y);
return (x+n)%n;
} void dfs1(int u,int fa)
{
int i,v;
cnt[u]=f[u]=;
for(i=;i<q[u].size();i++){
if((v=q[u][i])==fa)continue;
dfs1(v,u);
cnt[u]+=cnt[v];
f[u]=((f[u]*f[v])%MOD*rev[cnt[v]])%MOD;
}
f[u]=(f[u]*fac[cnt[u]-])%MOD;
} void dfs2(int u,int fa)
{
int i,v;
if(u!=){
f[u]=(f[fa]*cnt[u])%MOD*inv(n-cnt[u],MOD)%MOD;
ans=(ans+f[u]*f[u]%MOD)%MOD;
}
for(i=;i<q[u].size();i++){
if((v=q[u][i])==fa)continue;
dfs2(v,u);
}
} int main(){
// freopen("in.txt","r",stdin);
int i,j,a,b;
fac[]=;
for(i=;i<N;i++)fac[i]=(i*fac[i-])%MOD;
for(i=;i<N;i++)rev[i]=inv(fac[i],MOD);
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=;i<=n;i++)q[i].clear();
for(i=;i<n;i++){
scanf("%d%d",&a,&b);
q[a].push_back(b);
q[b].push_back(a);
} dfs1(,);
ans=f[]*f[]%MOD;
dfs2(,); printf("%I64d\n",(ans+MOD)%MOD);
}
return ;
}

HDU-4661 Message Passing 树形DP,排列组合的相关教程结束。

《HDU-4661 Message Passing 树形DP,排列组合.doc》

下载本文的Word格式文档,以方便收藏与打印。