Hdu 5378 Leader in Trees Land 2015 multi-school joint training tournament tree DP

Source: Internet
Author: User
Tags int size time 0 valid

Leader in the Tree land Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 169 Accepted Submission (s): 62


Problem Description Tree land had n cities, connected by n−1 roads. You can go to all city from any city. In the other words, this is a tree. The city numbered one are the root of this tree.

There is n ministers numbered from 1 to N. You'll send them to n cities, one city with one minister.

Since This was a rooted tree, each of which is a root of a subtree and there be n subtrees. The leader of a subtree is the minister with maximal number in this subtree. As can see, one minister can be the leader of several subtrees.

One day all the leaders attend a meet, you find that there is exactly K ministers. You want to know how many ways to send N ministers to each city so, there is K ministers attend the meet.

Give your answer mod 1000000007.

Input multiple test cases. The first line there are an integer T, indicating the number of test cases. For each test case, the first line contains the numbers n,k. Next N−1line Describe the roads of the tree land.

T=10,1≤n≤1000,1≤k≤n

Output for each test case, output one line. The output format is case # X:ans, and X is the case number,starting from 1.
Sample Input

2 3 2 1 2 1 3 10 8 2 1 3 2 4 1 5 3 6 1 7 3 8 7 9 7 10 6
Sample Output
Case #1:4 case #2:316512
Source multi-university Training Contest 7
To divide 1 to n into N nodes, the leader of the subtree is the most weighted point in this subtree.

Find the tree of N nodes, leading to the number of K cases.


The definition Dp[u][i] indicates that the subtree u selected the I-Leader case number.

Assuming that you have processed other subtrees of u, now compute the subtree V

Then dp[u][i] = Dp[u][j]*dp[v][i-j]

Transfer is only transferred from the active state, then the complexity is O (n^2)


For each heel, you can choose this point as the leader has 1 kinds of options, not choosing it as the leader has the size[u]-1 kind of plan.

So get two valid states.


For a subtree V, the scale is size[v] then you can choose the Size[v] point from the unused point, then

Sub-Tree V has c (size[v],size[u]-1-used points) in the scheme points. So the dp[v] needs to be multiplied by the number of combinations


The proof is as follows: Inductive method

For sub-trees with a subtree size K, the number of transfers <=k*k

Assuming that the complexity of the subtree satisfies the condition


The size of the subtree for U is K1,K2,K3 (this is assumed to be 3)

When processing subtree k1, transfer k1*2 times can be, get K1 kind of effective state (at this time 0 state actually useless, calculate K1 species can),

When handling K2, the number of transfers is K1*K2 times, (the nature of the backpack)

Then there is (K1+K2) a valid state, and the second shift is (K1+K2) the complexity of the *K3

The count is <= 2*k1 + k1*k2 + k1*k3+k2*k3 + k1*k1 + k2*k2 + k3*k3 (the square is due to the computational complexity of the subtree)

<= (K1+K2+K3) * (K1+K2+K3) = k1*k1 + k2*k2 + k3*k3 + 2*k1*k2 + 2*k1*k3 + 2*k2*k3

Obviously K1*K2+K1*K3+K2*K3 will be bigger than K1.

Proof complete.


#include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std;
#define MAXN 1001 #define LL long long ll mod = 1e9+7;
ll DP[MAXN][MAXN];
vector<int>head[maxn];
int SIZE[MAXN];
ll RES[MAXN];
ll C[MAXN][MAXN];
    void Dfs (int u,int f) {size[u] = 1;
        for (int i = 0;i < Head[u].size (); i++) {int v = head[u][i];
        if (v = = f) Continue;
        DFS (V,U);
    Size[u] + = Size[v];
    } dp[u][1] = 1;
    Dp[u][0] = size[u]-1;
    int e = 1;
        for (int i = 0;i < Head[u].size (); i++) {int v = head[u][i];
        if (v = = f) Continue;
        for (int j = 0;j <= size[v] + E; j + +) Res[j] = 0;
        for (int j = 0;j <= size[v]; j + +) dp[v][j] = Dp[v][j]*c[size[u]-e][size[v]]%mod;
            for (int k = e;k >= 0; k--) {if (dp[u][k] = = 0) continue;
            ll x = Dp[u][k]; for (int j = size[v];j >=0; j--) {Res[k+j] = (Res[k+j] + dp[v][j]*x)% MoD;
        }} E + = Size[v];
    for (int j = 0;j <= e; j + +) Dp[u][j] = Res[j];
    }} int main () {memset (c,0,sizeof (C));
        for (int i = 0;i < MAXN; i++) {c[i][0] = 1;
    for (int j = 1;j <= i; j + +) C[i][j] = (C[i-1][j] + c[i-1][j-1])%mod;
    } int t,n,k,tt=1;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &n,&k);
        Memset (Dp,0,sizeof (DP));
        for (int i = 0;i <= n; i++) head[i].clear ();
        int u,v;
            for (int i = 1;i < n; i++) {scanf ("%d%d", &u,&v);
            Head[u].push_back (v);
        Head[v].push_back (U);
        } memset (size,0,sizeof (size));
        DFS (1,0);
    printf ("Case #%d:%i64d\n", tt++,dp[1][k]);
} return 0;

 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.