CF 855C. Helga Hufflepuff's Cup【树形DP】
来源:互联网 发布:市场上主流单片机 编辑:程序博客网 时间:2024/06/06 04:28
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
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.
Input
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
Output a single integer, the number of ways of giving each vault a type following the conditions modulo 109 + 7.
Examples
Input
4 2
1 2
2 3
1 4
1 2
Output
1
Input
3 3
1 2
1 3
2 1
Output
13
Input
3 1
1 2
1 3
1 1
Output
0
Note
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.
题意:给你一棵树,可以染m种颜色(1~m),现在定义一种特殊颜色k,一棵树上最多能有x个特殊颜色,如果一个节点为特殊颜色k,那么他相邻的节点的值只能选比k小的颜色。现在问你一共有多少种染色的方法。
dp[i][j][k] 表示以i为根的子树上有j个特殊颜色结点的染色方案数,
k = 0 :i结点的颜色比k小
k = 1 :i结点的颜色为k
k = 2 :i结点的颜色大于k
#include<iostream>#include<cstring>#include<algorithm>#include<vector>#include<functional>using namespace std;#define ll long long#define inf 0x3f3f3f3fusing namespace std;typedef pair<int, int> P;const int MAXN = 100010;const int mod = 1e9 + 7;vector<int> mp[MAXN];ll N, M, K, X;ll dp[MAXN][11][3], sz[MAXN], tmp[11][3];void dfs(int u, int fa){ dp[u][0][0] = K - 1; dp[u][1][1] = 1; dp[u][0][2] = M - K; sz[u] = 1; int v; for (int i = 0; i < mp[u].size(); i++) { v = mp[u][i]; if (v == fa) continue; dfs(v, u); memset(tmp, 0, sizeof(tmp)); for (int j = 0; j <= sz[u]; j++) for (int k = 0; k <= sz[v]; k++) { if (j + k > X) continue; tmp[j + k][0] = (tmp[j + k][0] + dp[u][j][0] * (dp[v][k][0] + dp[v][k][1] + dp[v][k][2])) % mod; tmp[j + k][1] = (tmp[j + k][1] + dp[u][j][1] * dp[v][k][0]) % mod; tmp[j + k][2] = (tmp[j + k][2] + dp[u][j][2] * (dp[v][k][2] + dp[v][k][0])) % mod; } sz[u] = min(sz[u] + sz[v], X); for (int j = 0; j <= sz[u]; j++) for (int k = 0; k < 3; k++) dp[u][j][k] = tmp[j][k]; }}int main(){ int u, v; ll ans = 0; cin >> N >> M; for (int i = 1; i < N; i++) { scanf("%d %d", &u, &v); mp[u].push_back(v), mp[v].push_back(u); } cin >> K >> X; dfs(1, -1); for (int i = 0; i <= sz[1]; i++) for (int j = 0; j < 3; j++) ans = (ans + dp[1][i][j]) % mod; cout << ans << endl; return 0;}
- CF 855C. Helga Hufflepuff's Cup【树形DP】
- Codeforces 855 C. Helga Hufflepuff’s Cup (树形dp)
- CodeForces 855C Helga Hufflepuff's Cup(树形dp)
- codeforce 855C. Helga Hufflepuff's Cup 树形DP
- codeforces 855C Helga Hufflepuff's Cup (树形dp)
- Codefest 17 C. Helga Hufflepuff's Cup(树形DP)
- C. Helga Hufflepuff's Cup(树形dp)
- 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. 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 855CHelga Hufflepuff's Cup(树型DP)
- cf 682 C (树形dp)
- 树形dp(Centroids,cf 708C)
- CF 708C. Centroids 树形dp
- 【CF 736C】Ostap and Tree,树形DP
- this关键字
- JQuery基础
- 关押罪犯 codevs 1069(整理:加权并查集?+虚点并查集+二分图染色)
- SpringMVC学习笔记(5)-----标签库
- java初级阶段quickhit游戏
- CF 855C. Helga Hufflepuff's Cup【树形DP】
- 爬虫学习--基于selenium动态抓捕(一)安装
- Bringing up interface eth0: Device eth0 does not seem to be present, delaying initialization.
- 二叉搜索树的第K个结点
- js(question)
- Dagger2学习从这里开始
- MongoDB之Ubuntu16.04安装mongodb
- 如何输入多组测试数据
- Python(1) Anaconda流水账