Manthan, Codefest 17-C-Helga Hufflepuff's Cup(树形DP)
来源:互联网 发布:淘宝店铺怎么改折扣 编辑:程序博客网 时间:2024/06/05 13:26
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.
题意:给你n个点,n-1条边,让后给你个m,代表警戒值的最大值,再给你k和x,代表最优警戒值和最优警戒值的最大数量
然后让你给每个点定义一个警戒值。假如该点定义为最优警戒值,则与该点相连的点的警戒值不能定义为>=k的值。问你有多少种制定方案。
题解:
dp[i][j][0]:以i为根的子树中有j个最优警戒值,并且i点警戒值小于k
dp[i][j][1]:以i为根的子树中有j个最优警戒值,并且i点警戒值等于k
dp[i][j][2]:以i为根的子树中有j个最优警戒值,并且i点警戒值大于k
然后树形DP即可。
#include<set> #include<map> #include<stack> #include<queue> #include<vector> #include<string> #include<time.h>#include<math.h> #include<stdio.h> #include<iostream> #include<string.h> #include<stdlib.h> #include<algorithm> #include<functional> using namespace std; #define ll long long #define inf 1000000000 #define mod 1000000007 #define maxn 100005#define lowbit(x) (x&-x) #define eps 1e-9vector<int>q[maxn];int n,m,k,x,dp[maxn][12][3],g[12][3];void dfs(int u,int p){int i,j,t;dp[u][1][1]=1;dp[u][0][0]=k-1;dp[u][0][2]=m-k;for(t=0;t<q[u].size();t++){int v=q[u][t];if(v==p)continue;dfs(v,u);memset(g,0,sizeof(g));for(i=x;i>=0;i--)for(j=x-i;j>=0;j--){g[i+j][0]=(g[i+j][0]+1ll*dp[u][i][0]*dp[v][j][0])%mod;g[i+j][0]=(g[i+j][0]+1ll*dp[u][i][0]*dp[v][j][1])%mod;g[i+j][0]=(g[i+j][0]+1ll*dp[u][i][0]*dp[v][j][2])%mod;g[i+j][1]=(g[i+j][1]+1ll*dp[u][i][1]*dp[v][j][0])%mod;g[i+j][2]=(g[i+j][2]+1ll*dp[u][i][2]*dp[v][j][0])%mod;g[i+j][2]=(g[i+j][2]+1ll*dp[u][i][2]*dp[v][j][2])%mod;}for(i=0;i<=x;i++)for(j=0;j<=2;j++)dp[u][i][j]=g[i][j];}}int main(void){int xx,yy,i,j,ans=0;scanf("%d%d",&n,&m);for(i=1;i<n;i++){scanf("%d%d",&xx,&yy);q[xx].push_back(yy);q[yy].push_back(xx);}scanf("%d%d",&k,&x);dfs(1,0);for(i=0;i<=x;i++)for(j=0;j<=2;j++)ans=(ans+dp[1][i][j])%mod;printf("%d\n",ans);return 0;}
- 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)
- Codefest 17 C. Helga Hufflepuff's Cup(树形DP)
- Manthan, Codefest 17 C. Helga Hufflepuff's Cup
- Manthan, Codefest 17 C. Helga Hufflepuff's Cup
- Codeforces Manthan, Codefest 17 C. Helga Hufflepuff's Cup 树型DP
- Codeforces 855 C. Helga Hufflepuff’s Cup (树形dp)
- CodeForces 855C Helga Hufflepuff's Cup(树形dp)
- C. Helga Hufflepuff's Cup(树形dp)
- codeforces 855C Helga Hufflepuff's Cup (树形dp)
- Codeforces Manthan Codefest 17 C 855CHelga Hufflepuff's Cup(树型DP)
- CF 855C. Helga Hufflepuff's Cup【树形DP】
- codeforce 855C. Helga Hufflepuff's Cup 树形DP
- Manthan, Codefest 17 E. Salazar Slytherin's Locket 数位DP
- codeforces Manthan, Codefest 17 B Marvolo Gaunt's Ring(dp)
- Manthan, Codefest 17 E. Salazar Slytherin's Locket(数位DP)(进制)
- Manthan, Codefest 17 E. Salazar Slytherin's Locket(数位DP)
- Sending a URL to Another App on Android and iOS with Delphi XE5
- React-Router去掉后便跟随的k参数
- java创建对象
- spark2.0访问ES5.1中的数据
- tomcat avicon.ico
- Manthan, Codefest 17-C-Helga Hufflepuff's Cup(树形DP)
- AJAX学习笔记(三)_XMLHttpRequest向服务器发送请求
- React 组件基本使用(三) ---父子组件之间的通信
- 【Web】Javascript多项式计算器
- Cannot resolve symbol 'permission'
- Children's Game UVA
- Unknown error: Unable to build: the file dx.jar was not loaded from the SDK folder!
- Java 任务调度框架 Quartz《一》
- sed 命令 解析及使用