[牛 客 13611] Tree

tree

Link to topic: Niuke 13611 Tree

Description

Given a tree with \ (n \) nodes, there are \ (k \) pigments to dye the tree.
A coloring scheme is legal if and only for all pairs of points of the same color \ ((x, y) \) , the color of all points on the path satisfying \ (x \) to \ (y \) is the same (including \ (x \) and \ (y \) ).
Count the number of solutions, and the answer is modulo \ (1e9 + 7 \) .
Data range \ (1 \ le n, k \ le 300 \)

Solution

This is a conclusion question .
If it is done directly, it is obviously not easy to do, we consider switching perspectives.
Finding the point-to-point path on the tree can actually be converted to the chain through the \ (dfs \) order . Let's run \ (dfs \) once , and note the \ (dfs order \) \ (dfn [x] \) of each point . We will find that if the current traversal reaches the point \ (x \) , then the ancestors of \ (x \) must have traversed, and some of its brothers have been traversed. We specify that the order of \ (x \) and \ (y \) is arranged according to \ (dfn [y] <dfn [x] \) , then when we enumerate \ (x \) , \ (y \ ) Has been colored. If you want to traverse to \ (y \) , then \ (x \) must pass through its father anyway, so \ (x \) must be the same color as \ (father [x] \) . Therefore, we found that we do not care about the shape of this tree, what really matters is




\ (n \) and \ (k \) .
Well, next, we define \ (f [i] [j] \) to indicate the number of schemes in which the first \ (i \) nodes are dyed with \ (j \) colors, according to \ (dfs order \) . Consider how to transfer:

  • The \ (i \) node dyes one of these \ (j \) colors, then the number of schemes is \ (f [i-1] [j] \) because its color must be the same as its father ;
  • The \ (i \) node dyes a new color, then the number of schemes is \ (f [i-1] [j-1] * (k- (j-1)) \) because there are still \ ( k-j + 1 \) colors are available for \ (i \) selection.

Obviously the boundary condition is \ (f [0] [0] = 1 \) .
The answer is \ (\ sum_ {i = 1} ^ {k} f [n] [i] \) .
Time complexity \ (O (nk) \) , you can pass this question.
PS: Why is the data range for this question so small ...

Code

// Author: wlzhouzhuan
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define rint register int
#define rep(i, l, r) for (rint i = l; i <= r; i++)
#define per(i, l, r) for (rint i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)
#define Each(i) for (rint i = head[u]; i; i = edge[i].nxt)

inline int read() {
  int x = 0, neg = 1; char op = getchar();
  while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
  while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); }
  return neg * x;
}
inline void print(int x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

const int mod = 1e9 + 7;
const int N = 301;
int f[N][N];
int n, k;

int main() {
  n = read(), k = read();
  f[0][0] = 1;
  for (rint i = 1; i <= n; i++) {
    for (rint j = 1; j <= k; j++) {
      f[i][j] = (f[i - 1][j] + 1ll * f[i - 1][j - 1] * (k - j + 1) % mod) % mod;
    }
  }
  int ans = 0;
  for (rint i = 1; i <= k; i++) {
    ans = (ans + f[n][i]) % mod;
  }
  printf("%d\n", ans); 
  return 0;
}

Guess you like

Origin www.cnblogs.com/wlzhouzhuan/p/12674003.html