动态规划(DP计数):HDU 5121 Just A Mistake
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
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
(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
#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 ;
}
动态规划(DP计数):HDU 5121 Just A Mistake的更多相关文章
- HDU 5121 Just A Mistake
Just A Mistake Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) ...
- 动态规划(DP计数):HDU 5116 Everlasting L
Matt loves letter L.A point set P is (a, b)-L if and only if there exists x, y satisfying:P = {(x, y ...
- HDU 4055 The King’s Ups and Downs(DP计数)
题意: 国王的士兵有n个,每个人的身高都不同,国王要将他们排列,必须一高一矮间隔进行,即其中的一个人必须同时高于(或低于)左边和右边.问可能的排列数.例子有1千个,但是最多只算到20个士兵,并且20个 ...
- HDU 4055 Number String(DP计数)
题意: 给你一个含n个字符的字符串,字符为'D'时表示小于号,字符为“I”时表示大于号,字符为“?”时表示大小于都可以.比如排列 {3, 1, 2, 7, 4, 6, 5} 表示为字符串 DIIDID ...
- 动态规划dp
一.概念:动态规划dp:是一种分阶段求解决策问题的数学思想. 总结起来就一句话:大事化小,小事化了 二.例子 1.走台阶问题 F(10):10级台阶的走法数量 所以:F(10)=F(9)+F(8) F ...
- 【POJ1952】逢低吸纳 dp+计数
题目大意:给定一个有 N 个数的序列,求其最长下降子序列的长度,并求出有多少种不同的最长下降子序列.(子序列各项数值相同视为同一种) update at 2019.4.3 题解:求最长下降子序列本身并 ...
- 算法-动态规划DP小记
算法-动态规划DP小记 动态规划算法是一种比较灵活的算法,针对具体的问题要具体分析,其宗旨就是要找出要解决问题的状态,然后逆向转化为求解子问题,最终回到已知的初始态,然后再顺序累计各个子问题的解从而得 ...
- Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)
题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...
- 动态规划(DP计数):HDU 5117 Fluorescent
Matt, a famous adventurer who once defeated a pack of dire wolves alone, found a lost court. Matt fi ...
随机推荐
- java内存
java内存分为四部分: 1).栈区(stacksegment),由编译器自动分配释放,存放函数的参数值和局部变量的值等,具体方法执行结束之后,系统自动释放JVM内存资源: 2).堆区(heapseg ...
- 关于GC进行垃圾回收的时机
前言 今天查看一个同事的代码,发现代码中多处地方使用了GC.Collect()方法,我问他为什么这么做,他说感觉程序中定义了好多变量,怕GC回收不及时,用GC.Collect()可以手动掌控GC进行垃 ...
- Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGB
Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGB ...
- spring学习笔记之spring框架本质
Spring框架的本质是,开发者在Spring配置文件中使用XML元素进行配置,实际驱动Spring执行相应的代码: 使用<bean.../>元素,实际启动Spring执行无参或有参构造函 ...
- ReactNative-----环境搭建(android)
1.参考文档 http://reactnative.cn/docs/0.26/getting-started.html http://reactnative.cn/docs/0.26/running- ...
- 命令行下上传文件到iOS软件 专业文件管理/gplayer
U盘丢了, 就拿手机当U盘用用先. 一般情况下软件打开上传功能, 在浏览器里上传即可. 可是偏偏我的电影放在了 树莓派里面(搭建了一个SMB), 直接浏览器的话,会多占用些带宽, 我的破路由器.... ...
- 【BZOJ2793】【数学】[Poi2012]Vouchers
Description 考虑正整数集合,现在有n组人依次来取数,假设第i组来了x人,他们每个取的数一定是x的倍数,并且是还剩下的最小的x个. 正整数中有m个数被标成了幸运数,问有哪些人取到了幸运数. ...
- HTTP中Get与Post、ViewState 原理
Http是请求,响应的模型,服务器不会来读取浏览器的网页,只能够得到客户端提交过来的数据当用户点击提交,服务器就知道"提交回来了"(PostBack) Get与Post 设置for ...
- HTML XML XHTML DHTML区别与联系
(1)HTML HTML是超文本标记语言 (2)XML XML是可扩展标识语言,但XML是一种能定义其他语言的语. XML最初设计的目的是弥补HTML的不足, 以强大的扩展性满足网络信息发布的需要 , ...
- C#中的反射 Assembly.Load() Assembly.LoadFrom()
一些关于C#反射的知识,估计也就最多达到使用API的程度,至于要深入了解,以现在的水平估计很难做到,所以下面此篇文章,以作为一个阶段的总结. 对于反射的总结,我想从以下几个方面展开,首先是反射程序集, ...