动态规划(DP计数):HDU 5121 Just A Mistake

As we all know, Matt is an outstanding contestant in ACM-ICPC. Graph problems are his favorite.

Once, he came up with a simple algorithm for finding the maximal independent set in trees by mistake.

A tree is a connected undirected graph without cycles, and an independent set is subset of the vertex set which contains no adjacent vertex pairs.

Suppose that the tree contains N vertices, conveniently numbered by 1,2, . . . , N. First, Matt picks a permutation p1, p2, . . . , pN of {1, 2, 3, . . . , N } randomly and uniformly.

After picking the permutation, Matt does the following procedure.

1.Set S = ∅.
2.Consider the vertex p1, p2, . . . , pN accordingly. For vertex pi, if and only if there is no vertex in S which is adjacent to pi, add vertex pi into S.
3.Output the set S.

Clearly the above algorithm does not always output the maximal independent set. Matt would like to know the expected size of set S instead.

 

Input

The first line contains only one integer T , which indicates the number of test cases.

For each test case, the first line contains an integer N (1 ≤ N ≤ 200), indicating the number of vertices in the graph.

Each of the following N - 1 lines contains two integers u, v (1 ≤ u, v ≤ N ) indicating an edge between u and v. You may assume that all the vertices are connected.

 

Output

For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y is the answer. To avoid rounding error, the answer you should output is:

(the expected size of independent set) × N! mod (109 + 7)

 

Sample Input

2
4
1 2
1 3
1 4
3
1 2
2 3

Sample Output

Case #1: 60
Case #2: 10

Hint

In the first sample, there are 4 vertices, so there are 4! permutations Matt may get. Suppose the permutation Matt gets is 1 2 3 4. He will add vertex 1 into the independent set. Suppose the permutation Matt gets is 2 1 3 4. He will add vertex 2, vertex 3 and vertex 4 into the independent set. It is obvious that if the first element in the permutation is not vertex 1, he will get an independent set whose size is 3. Otherwise, he well get an independent set whose size is 1. Since there are 18 permutations whose first element is not vertex 1, the answer in the first sample is (3 × 18 + 1 × 6) mod (10^9 + 7) = 60.
  好题好题,我有幸成为了HDU第12名A穿此题的人。
  这道题就是对任意1~n的排列跑题中所给的程序,得到的集合S,求sigma(|S|)。
  考虑dp,dp[x][i]表示x在当前以它为根的子树中的所有1~sz[x]的排列中位置在i,并且x被选入集合的排列数。
  枚举根节点,发现不同子树间互不影响,只有父亲与儿子之间才会有影响,现在就是dp的形式就是两组排列,相互安插,用组合数搞一搞。
  枚举to[i]这棵子树的1~sz[to[i]]的集合中前a个插在x前面,后面的sz[to[i]]-a个在x后面,乘上dp[x][i],再乘上选位置的方案,选位置的话,x个数插到y个数中,方案数是C(x+y,x)
  时刻要记得取模……         貌似我的题解是此题网络上的第一篇BLOG,好开心……
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=,Mod=(int)1e9+;
int cnt,fir[N],to[N*],nxt[N*];
void addedge(int a,int b){
nxt[++cnt]=fir[a];to[fir[a]=cnt]=b;
nxt[++cnt]=fir[b];to[fir[b]=cnt]=a;
}
typedef long long LL;
#define clr(x) memset(x,0,sizeof(x))
LL ans,dp[N][N],tmp[N],sum[N],fac[N],c[N][N];
LL Inv(LL x){return x==?:(Mod-Mod/x)*Inv(Mod%x)%Mod;}
void Prepare(){
fac[]=;
for(int i=;i<N;i++)
fac[i]=fac[i-]*i%Mod;
for(int i=;i<N;i++)for(int j=;j<=i;j++)
c[i][j]=fac[i]*Inv(fac[j]*fac[i-j]%Mod)%Mod;
} int fa[N],sz[N];
void Update(int x,int y){
clr(sum);clr(tmp);
for(int i=;i<=sz[y];i++)
sum[i]=(sum[i-]+dp[y][i])%Mod; for(int i=;i<=sz[x];i++)
for(int j=;j<=sz[y];j++){
LL a=dp[x][i]*(((fac[sz[y]]-sum[j])%Mod+Mod)%Mod)%Mod;
LL b=c[i+j-][j]*c[sz[x]+sz[y]-i-j][sz[y]-j]%Mod;
(tmp[i+j]+=a*b%Mod)%=Mod;
}
sz[x]+=sz[y];
for(int i=;i<=sz[x];i++)
dp[x][i]=tmp[i];
} void DP(int x,int f){
if(fa[x]==f)return;
clr(dp[x]);sz[x]=;
fa[x]=f;dp[x][]=;
for(int i=fir[x];i;i=nxt[i])
if(to[i]!=f){
DP(to[i],x);
Update(x,to[i]);
}
} int st[N],top;
void DFS(int x,int f){
st[++top]=x;
for(int i=fir[x];i;i=nxt[i])
if(to[i]!=f)DFS(to[i],x);
}
int T,cas,n,a,b;
void Init(){
clr(fir);clr(fa);
top=ans=cnt=;
} int main(){
Prepare();
scanf("%d",&T);
while(T--){
Init();
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&a,&b);
addedge(a,b);
}
DFS(,);
for(int i=;i<=n;i++){
DP(st[i],-);
for(int j=;j<=n;j++)
(ans+=dp[st[i]][j])%=Mod;
}
printf("Case #%d: %lld\n",++cas,ans);
}
return ;
}
 
上一篇:WIN10-LTSC-2021开机wsappx占用CPU解决办法


下一篇:加入域提示找不到网络路径活动目录缺少dns记录”