动态规划(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 ...
随机推荐
- iOS 设置图片imageView圆角——对图片进行裁剪
以前设置图片圆角总是把imageView设置成圆形,然后设置maskToBounds为YES,其实这样处理很消耗性能,图片多了之后比较卡,最好将图片进行裁剪后显示:这里有个分类可以用: UIImage ...
- C++实现一个多线程同步方式的协同工作程序示例
多线程并发程序与协同程序其实是不同的概念.多线程并发是多个执行序同时运行,而协同程序是多个执行序列相互协作,同一时刻只有一个执行序列.今天想到的是将两者结合起来,拿现实生活中的例子来说,假设一个班级有 ...
- C# 制作卸载文件
1.建一个控制台应用程序Uninstall: 2.在应用程序的mian方法中添加 static void Main(string[] args) { System.Diagnostics.Proces ...
- jquery中onclick="fn"中$(this)所代表的对象
jquery中onclick="fn"中$(this)所代表的对象 js方法 function qiehuan(){ var src = $(this).attr("da ...
- js 数组 var arr=[] 的用法总结
刚接触了一个js数组,用起来很头疼,总结一下 基本格式: var arr = [[name,value],[name2,value2],[name3,value3]]; 如何给 arr 动态添加元素 ...
- 【 java版坦克大战--事件处理】 坦克动起来了
折腾了这么久,坦克总算能动了.只贴代码编辑不给上首页,花了半个小时的时间写了n多注释. 再顺便把绘图的原理发在这里: 绘图原理 Component类提供了两个和绘图有关的重要方法: ① paint ...
- 【java版坦克大战---准备篇】 java 绘图
要写坦克大战当然要先画出坦克.java画图是基础. package com.game; import java.awt.*; import javax.swing.*; public class Pr ...
- 有关sybase的一些零星经验
clear transaction log >dump transaction master with truncate_only >dump transaction master wit ...
- #Leet Code# Convert Sorted Array to Binary Search Tree
描述:递归 代码: class Solution: # @param num, a list of integers # @return a tree node def sortedArrayToBS ...
- 【C语言】中的布尔类型
C语言中的布尔类型 一.相关基础知识 首先bool true false为C++中的关键字,C语言中默认不支持这几个字符! 二.具体内容 在C89 (ANSI C)标准中没有定义与布尔类型相关的内 ...