Manthan, Codefest 17 C. Helga Hufflepuff's Cup(树形DP)

 

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

 

 

 

题意:给你一棵树,可以染m种颜色,现在定义一种最高值k,一棵树上最多能有x个最高值,如果一个节点为最高值k,那么他相邻的节点的值只能选比他小的。现在问你一共有多少种染色的方法。

 

思路:dp [i] [j] [k] 表示以i节点为根的子树上选j个最高值有多少种方法,k = 0表示选比K小的值,k = 1表示选值为k,k = 2表示选比k大的值

 

 

#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long

using namespace std;

const int mod = 1e9 + 7;


int n,m,K,X;
vector<int> e[100010];
LL dp[100010][12][3];
int sz[100010];
int t[12][3];

LL dfs(int x,int pre)
{
    dp[x][0][0] = K-1;
    dp[x][1][1] = 1;
    dp[x][0][2] = m-K;
    sz[x] = 1;
    for(int i=0;i<e[x].size();i++)
    {
        int xx = e[x][i];
        if(xx == pre)
            continue;
        dfs(xx,x);
        memset(t,0,sizeof(t));
        for(int j=0;j<=sz[x];j++)
            for(int k=0;k<=sz[xx];k++)
            {
                if(j+k > X)
                    continue;
                t[j+k][0] = (t[j+k][0] + dp[x][j][0]*(dp[xx][k][0] + dp[xx][k][1] + dp[xx][k][2])%mod)%mod;
                t[j+k][1] = (t[j+k][1] + dp[x][j][1]*(dp[xx][k][0])%mod)%mod;
                t[j+k][2] = (t[j+k][2] + dp[x][j][2]*(dp[xx][k][0] + dp[xx][k][2])%mod)%mod;
            }
        sz[x] = min(sz[x]+sz[xx],X);
        for(int j=0;j<=sz[x];j++)
            for(int k=0;k<3;k++)
                dp[x][j][k] = t[j][k];
    }
}
int main(void)
{
    int i,j,k;
    while(scanf("%d%d",&n,&m)==2)
    {
        for(i=1;i<=n;i++)
            e[i].clear();
        for(i=1;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            e[x].push_back(y);
            e[y].push_back(x);
        }
        scanf("%d%d",&K,&X);
        dfs(1,-1);
        LL ans = 0;
        for(j=0;j<=X;j++)
            for(k=0;k<3;k++)
                ans = (ans + dp[1][j][k])%mod;
        cout << ans << endl;
    }
}

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SEO建设者,终极白帽SEO软件,四大功能完美呈现:   1.关键词优化   *长尾关键词查询-网络独家!*   可以查询出任意关键词的所有百度相关关键词(多可达上千),同时辅助显示关键词搜索量(百度指数)、收录量、优化率等。在百度相继取消相关关键词服务,和关键词指数flash显示后,本软件是网络上唯一可以同时查询这两项的优化工具了。   *百度关键词指数查询*   可查询关键词的详细百度指数信息,包括当天搜索量、月搜索量、百度,谷歌收录量、优化指数、用户关注度等。同时显示分地区的详细搜索量,搜索量按年龄、职业、学历分布等。   *关键词竞争度分析*   通过分析指定数量关键词竞争对手的相关数据(PR/百度首页更新/独立网站数等),从而总结出该关键词在百度搜索引擎上的优化难易程度。   *关键词排名查询-超强功能!*   支持目前主流的七大搜索引擎(百度、谷歌、雅虎、搜狗、搜搜、有道、必应)的关键词排名查询。支持网站与关键词的一对多与多对多两种联动查询模式,同时支持关键词排名历史记录保存,关键词收藏等功能。   *百度热门关键词查询*   分类列出所有百度热门关键词列表,查询热门关键词的同时,辅以显示百度搜索量,优化指数等。   2、链接优化   *友情链接管理*   同时管理你的多个网站的友情链接,方便检测友链网站的PR、收录情况等SEO指数,以及对方网站是否正确做本站链接。提供友链检测历史记录查询,让你对友链网站的SEO指数变化情况一目了然。   *网站黑链检测-网络首家!*   黑链是近年兴起的一种黑客方式的链接交易模式,即黑客攻破服务器并修改网站内容,出售网站的隐形链接。越来越多的黑链流向市场,黑链的存在将使您的网站严重降权,甚至被K,您的网站上是否也存在黑链?本软件是网络上首家推出检测网站黑链的工具。   *网站外链分析*   是否苦恼竞争对手比贵站的排名要高,SEO更有成效?原因很简单,他们比你拥有更多更好的外部链接。怎么办?通过本软件的网站外链分析功能,快速了解竞争对手的所有外链情况,对症下药,各个击破,轻松超越竞争对手。   *友情链接市场*   按分类查询多个知名友情链接市场(网站或论坛)的友情链接信息,并可以对链接信息按照pr、alexa排名、站长QQ在线等进行过滤。独特功能:创新的判断站长QQ是否在线功能,让你快速方便的与在线站长即时沟通,快速交换或购买链接。   *外链资源库*   包含了共七大类涵盖中英文网站的共计一万多个可以获取网站外链的网站资源。站长们可以根据自己的网站特性,挑选合适的资源,进行适当的操作,快速增加网站外链。   3、SEO体检   *常规体检*   SEO常规体检可以让网站主快速了解自己网站的一些SEO的基本信息,包括网站服务器信息、SEO指数信息、在各大搜索引擎总的收录反链信息等。   *高级体检*   SEO高级体检检测网站的主要关键词的排名及其收录量、搜索量等信息;检测网站的友情链接的安全情况;检测网站同服务器上的其他网站的SEO安全情况。   *整站死链接检测*   以树形目录结构的方式,遍历列出整站所有的的链接,并检测所有存在的死链接,并详细分析每个死链接的链接深度,导入链接等,让你快速清除网站存在的所有死链接。   *整站PR检测*   以树形目录结构的方式,遍历列出整站所有的链接,并分析每个链接的PR。让你对整个网站所有网页的PR值一目了然。   4、站长工具   *SEO综合工具*   包含了关键词密度查询、网页性噪比查询、网页META信息检测、搜索引擎模拟抓取等4款SEO相关的站长工具。   *链接/收录类工具*   包含了网站友情链接检测、网站收录反链查询、百度近期收录查询、谷歌近期收录查询等4款链接收录类站长工具。   *域名/IP类工具*   包含了CN到期域名查询、Whois查询、IP地址定位、同IP(服务器)所有网站查询等4款域名IP类站长工具。   *代码转换工具*   包含了繁简体转化、8进制加解密、Escape加解密、UrlEncode加解密、Html/JS互转等10款代码转换类站长工具。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值