Link
Source: Click to open link
Test instructions
Given n K
The following line n-1 a tree.
Fill the node of the tree with the number 1-n.
After filling out the number of leader nodes calculated, if this point is leader, then the number of points on this point > the number of sub-trees on this point
Q: There is exactly the number of fill schemes for K-leader nodes.
Ideas:
DP[I][J] indicates that a subtree with an I-point Root has a scheme number of exactly J leader.
If u is a leaf node then dp[u][0] = 0, dp[u][1] = 1;
If u is not a leaf node:
Do not consider whether U point can become leader, backpack a bit.
Then consider the U point: If you can become leader, set Siz[u] represents the number of subtree nodes of U.
So for u subtree, to put [1, siz[u]] to fill the subtree, when u is leader, u can only fill siz[u]
and the sub-tree allocation scheme is a multiset arrangement, because the sub-tree is assigned to a combination, the sub-tree is arranged.
The number of methods that set U as leader is X1
X1 = (siz[u]-1)! /siz[v1]! /siz[v2]!
Then dp[u][i] = dp[u][i] * (the total number of sub-trees of the U-X1) + dp[u][i-1] * x1
#pragma COMMENT (linker, "/stack:102400000,102400000") #include <iostream> #include <cstdio> #include < algorithm> #include <string> #include <cmath> #include <cstring> #include <queue> #include <set> #include <map> #include <vector>using namespace std;template <class t>inline BOOL Rd (T &ret) {char c; int sgn;if (c = GetChar (), c = = EOF) return 0;while (c! = '-' && (c< ' 0 ' | | c> ' 9 ')) C = ge TCHAR (); sgn = (c = = '-')? -1:1;ret = (c = = '-')? 0: (C-' 0 '); while (c = GetChar (), C >= ' 0 ' &&c <= ' 9 ') ret = ret * + (C-' 0 '); ret *= Sgn;return 1;} Template <class t>inline void pt (T x) {if (x < 0) {Putchar ('-'); x =-X;} if (x > 9) pt (X/10);p Utchar (x% 10 + ' 0 ');} typedef pair<int, int> pii;typedef long long ll;const int N = 1005;const int mod = 1e9 + 7;const int inf = 1e9;int P ow (int x, int y) {int ans = 1;while (y) {if (Y & 1) ans = (ll) ans*x%mod;y >>= 1;x = (ll) X*x%mod;} Return aNS;} Vector<int>g[n];int N, K;int dp[n][n], siz[n], lef[n];int a[n];int mul (int x, int y) {x = (ll) X*y%mod;return X;} inline void Add (int &x, int y) {x + = y; if (x >= mod) x-= mod;} inline void Sub (int &x, int y) {x-= y; if (x < 0) x + = mod;} inline void dv (int &x, int y) {x = (ll) x*pow (y, mod-2)% MoD;} int g[n];void dfs (int u, int fa) {Siz[u] = 1; Lef[u] = 0;for (auto V:g[u]) {if (v = = FA) Continue;dfs (V, u); siz[u] + = Siz [v];} if (siz[u] = = 1) {dp[u][0] = 0; dp[u][1] = 1;lef[u] = 1;return;} Dp[u][0] = 1;int x1 = a[siz[u]-1], x2 = a[siz[u]];siz[u] = 0;for (auto V:g[u]) {if (v = = FA) continue;for (int i = lef[ U] + lef[v]; I <= min (k, siz[u] + siz[v]); i++) G[i] = 0;for (int i = lef[u]; I <= min (k, siz[u]), i++) {for (int j = lef[v]; J <= min (k, siz[v]) && i + J <= K; J + +) {Add (G[i + j], Mul (Dp[v][j], dp[u][i]));}} for (int i = Lef[u] + lef[v]; I <= min (k, siz[u] + siz[v]); i++) dp[u][i] = G[i];siz[u] + = Siz[v];lef[u] + lef[v];d V (x1 , A[siz[v]]);DV (x2, A[siz[v]);} Siz[u]++;sub (x2, X1); for (int i = min (Siz[u], k); I >= lef[u]; i--) {int tmp = 0;ADD (tmp, Mul (Dp[u][i], x2)); if (i-1 >= Lef[u]) Add (TMP, Mul (dp[u][i-1], x1));DP [u][i] = tmp;}} int main () {a[0] = 1;for (int i = 1; i < N; i++) A[i] = (LL) a[i-1] * I%mod;int T, Cas = 1; rd (T); while (t--) {rd (n); r D (k); for (int i = 1; I <= n; i++) g[i].clear (), memset (Dp[i], 0, sizeof dp[i]); for (int i = 1, u, v; i < n; i++) {Rd ( u); RD (v); G[u].push_back (v); G[v].push_back (u);} DFS (1, 1);p rintf ("Case #%d:", cas++);p T (Dp[1][k]); Puts ("");} return 0;} /*995 4ans:124 31 22 32 44 41 22 32 4*/
Leader in the Tree landTime
limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 262 Accepted Submission (s): 88
Problem Descriptiontree Land have 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, your find that there is exactly k Ministers. Want to know how many ways to send n Ministers to all city so, there are k Ministers attend the meet.
Give your answer mod 1000000007 .
Inputmultiple test Cases. In the first line there are an integer T , indicating the number of test cases. For each test case, first line contains numbers n,k . Next n? 1 Line describe the roads of the tree land.
t = 10 , 1 ≤ n ≤ 1000 , 1 ≤ k ≤
Outputfor each test case, output one line. The output format is case # x : ans , x Is the case number,starting from 1 .
Sample Input
23 21 21 310 82 13 24 15 36 17 38 79 710 6
Sample Output
Case #1:4Case #2:316512
Source2015 multi-university Training Contest 7
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5378 Leader in tree land backpack