Codeforces Manthan, Codefest 17 C. Helga Hufflepuff's Cup 树型DP
来源:互联网 发布:随机抽取数字软件 编辑:程序博客网 时间:2024/06/05 16:53
Harry, Ron and Hermione have figured out that Helga Hufflepuff's cup is a horcrux. Through her encounter with Bellatrix Lestrange, Hermione came to know that the cup is present in Bellatrix's family vault in Gringott's Wizarding Bank.
The Wizarding bank is in the form of a tree with total n vaults where each vault has some type, denoted by a number between 1 to m. A tree is an undirected connected graph with no cycles.
The vaults with the highest security are of type k, and all vaults of type k have the highest security.
There can be at most x vaults of highest security.
Also, if a vault is of the highest security, its adjacent vaults are guaranteed to not be of the highest security and their type is guaranteed to be less than k.
Harry wants to consider every possibility so that he can easily find the best path to reach Bellatrix's vault. So, you have to tell him, given the tree structure of Gringotts, the number of possible ways of giving each vault a type such that the above conditions hold.
The first line of input contains two space separated integers, n and m — the number of vaults and the number of different vault types possible. (1 ≤ n ≤ 105, 1 ≤ m ≤ 109).
Each of the next n - 1 lines contain two space separated integers ui and vi (1 ≤ ui, vi ≤ n) representing the i-th edge, which shows there is a path between the two vaults ui and vi. It is guaranteed that the given graph is a tree.
The last line of input contains two integers k and x (1 ≤ k ≤ m, 1 ≤ x ≤ 10), the type of the highest security vault and the maximum possible number of vaults of highest security.
Output a single integer, the number of ways of giving each vault a type following the conditions modulo 109 + 7.
4 21 22 31 41 2
1
3 31 21 32 1
13
3 11 21 31 1
0
In test case 1, we cannot have any vault of the highest security as its type is 1 implying that its adjacent vaults would have to have a vault type less than 1, which is not allowed. Thus, there is only one possible combination, in which all the vaults have type 2.
一棵树需要给所有的节点分配点权,每个点可以取1到m间任意一个数,要求若某点点权为k,则与其相邻的点点权必须小于k,且最多只能标注x个点的点权为k。问有多少种分配点权的方式。
树型DP。
dp[i][j][l]表示以 i 为根的子树上,选了 j 个节点的权值为k的方案数。第 i 号节点的权值等于k时l=0,若小于k则l=1,大于k则l=2.
一开始时,i号点可以取1到m间所有数,所以dp[i][1][0] = 1; dp[i][0][1] = p - 1; dp[i][0][2] = m - p;
向上DP时,每有一个儿子节点to,我们就将之前的方案总数加上这个儿子的子树的贡献。转移方程如下:
(0<=x<=j,0<=k<=j)
dp[now][j][0] = ∑dp[to][j - k][1]*dp[now][k][0]; 当前节点选k,那么儿子的子树所有点权值只能小于k
dp[now][j][1]=∑(dp[to][j-k][0]+dp[to][j-k][1]+dp[to][j-k][2])*dp[now][k][1]; 当前节点小于k,儿子节点可以随意取
dp[now][j][2] =∑ (dp[to][j-k][1] + dp[to][j-k][2])*dp[now][k][2] ; 当前节点大于k,则儿子节点权值不能为k
为了避免一次转移当中,已经转移的状态的数值对未转移状态的影响,需要在每次转移当中用新数组记录,转移后再把DP的值移动过去。
#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=100005, inf = 0x3f3f3f3f;const ll llinf = 0x3f3f3f3f3f3f3f3f,mod=1000000007;const ld pi = acos(-1.0L);vector<int> v[maxn];ll dp[maxn][11][3],a[maxn][11][3];bool visit[maxn];int n;ll m, p, x;void dfs(int now) {visit[now] = 1;int size = v[now].size(),i,j,k,last=-1;dp[now][1][0] = 1; dp[now][0][1] = p - 1; dp[now][0][2] = m - p;for (i = 0; i < size; i++) {int to = v[now][i];if (!visit[to]) {dfs(to);for (j = 0; j <= x; j++) {for (k = 0; k <= 2;k++) {a[now][j][k] = 0;}}for (j = 0; j <= x; j++) {for (k = j; k >= 0;k--) {if (j != 0&&k!=0) a[now][j][0] += dp[to][j - k][1]*dp[now][k][0];a[now][j][1]+=((dp[to][j-k][0]+dp[to][j-k][1]+dp[to][j-k][2])*dp[now][k][1])%mod;a[now][j][2] += ((dp[to][j-k][1] + dp[to][j-k][2])*dp[now][k][2]) % mod;a[now][j][1] %= mod; a[now][j][2] %= mod; a[now][j][0] %= mod;}}for (j = 0; j <= x; j++) {for (k = 0; k <= 2;k++) {dp[now][j][k] = a[now][j][k];}}}}}int main() {int s,t,i,j,f;scanf("%d%I64d", &n, &m);for (i = 1; i < n; i++) {scanf("%d%d", &s, &t);v[s].push_back(t);v[t].push_back(s);}scanf("%I64d%I64d", &p, &x);mem0(dp); mem0(visit);dfs(1);ll sum = 0;for (i = 0; i <= x; i++) {for (j = 0; j <= 2; j++) {//if (i == 0 && j == 0) continue;sum += dp[1][i][j];sum %= mod;}}/*for (i = 1; i <= n; i++) {for (j = 0; j <= x; j++) {for (f = 0; f <= 2; f++) printf("%d %d %d %lld\n", i, j, f, dp[i][j][f]);}}*/printf("%I64d\n", sum);//system("pause");return 0;}
- Codeforces Manthan, Codefest 17 C. Helga Hufflepuff's Cup 树型DP
- Manthan, Codefest 17 C. Helga Hufflepuff's Cup
- Manthan, Codefest 17 C. Helga Hufflepuff's Cup
- Manthan, Codefest 17 C. Helga Hufflepuff's Cup(树形DP)
- Manthan, Codefest 17: C. Helga Hufflepuff's Cup(树形DP)
- Manthan, Codefest 17-C-Helga Hufflepuff's Cup(树形DP)
- Codeforces Manthan Codefest 17 C 855CHelga Hufflepuff's Cup(树型DP)
- Codefest 17 C. Helga Hufflepuff's Cup(树形DP)
- Codeforces 855 C. Helga Hufflepuff’s Cup (树形dp)
- CodeForces 855C Helga Hufflepuff's Cup(树形dp)
- codeforces 855C Helga Hufflepuff's Cup (树形dp)
- CF 855C. Helga Hufflepuff's Cup【树形DP】
- C. Helga Hufflepuff's Cup(树形dp)
- codeforce 855C. Helga Hufflepuff's Cup 树形DP
- codeforces Manthan, Codefest 17 B Marvolo Gaunt's Ring(dp)
- codeforces Manthan, Codefest 17 B.Marvolo Gaunt's Ring (简单dp)
- Manthan, Codefest 17 E. Salazar Slytherin's Locket 数位DP
- Manthan, Codefest 17 Marvolo Gaunt's Ring
- 从零起步做到Linux运维经理, 你必须管好的23个细节
- Recyclerview----- findViewByPosition getChildAt
- Java 基础语法
- Mac下java 调用C
- Servlet上传zip文件并解压
- Codeforces Manthan, Codefest 17 C. Helga Hufflepuff's Cup 树型DP
- Word
- A端,B端,C端
- v-on 绑定自定义事件
- Javascript 中的arguments 学习
- js for in 和for of的区别
- Scala编程实战—字符串
- TP5 接收表单参数 input
- 安卓手机1px问题