9.28 Binary Count

The meaning of problems

Given a \ (N \) binary tree nodes and their reference numerals, reference numeral as follows: No. \ (I \) node in the binary tree of a preorder traversal of the section is exactly \ (I \) th occurrence

Defined \ (A_i \) represents the number \ (I \) position of a point appearing in the binary tree in preorder

Now, given \ (M \) limitation conditions, the \ (I \) limitation \ (<u_i, v_i> \ ) represents \ (A_ u_i {} <V_I A_ {} \) , i.e. in preorder \ (u_i \) in \ (v_i \) appears before

Calculate the number of different binary numbers with all satisfy the above restrictions, modulo \ (9 + 10 ^ 7 \)

\ (N \ leq 500 \)


solution

In the case without considering the constraints, the obvious answer is the number of Cattleya

Irrespective of the number of Cattleya, try \ (DP \) to solve this problem, first of all we have a \ (3) \ O (N ^) of \ (DP \)

Can be found for nodes \ (X \) , which is provided in the left subtree of size \ (A \) , the right subtree of size \ (B \)

Then the left subtree is the set number \ (. 1 + X \ X to A + \) , the right subtree \ (x + a + 1 \ to x + a + b + 1 \)

So we set \ (f [x] [y ] \) is to \ (x \) for the root, the size of \ (y \) answer subtree

Then transferred very simple: we then enumerate subtree size enumeration left subtree size, transferred multiplication principle
\ [f [x] [y ] = \ sum_ {i = 0} ^ yf [x + 1 ] [i] \ times f [
x + i + 1] [yi] \] we are in the \ (DP \) plus restrictions based on, then it means that we only meet in all cases be transferred restrictions

Observe every restriction: If you want to preorder in \ (u \) appears in the \ (v \) before, only three cases:

  • \ (u \) in \ (v \) left subtree
  • \ (v \) in \ (u \) right sub-tree
  • For \ (u \) and \ (v \) most recent common ancestor \ (w \) , \ (u \) in \ (w \) left subtree, \ (v \) in \ (w \) right subtree

Can be found in the sub-tree numbers are continuous, which tells us a similar manner prefix and queries whether the limit

Because of limitations is two related sub-tree, we opened a two-dimensional array to record all restrictions, then the query whether there are restrictions in effect means that the query within these two sub-tree corresponding to the tree if there are two sub-rectangular form of ID value

We recorded a two-dimensional prefix and can \ (O (1) \) judgment


Code

#include <cstdio>
#include <cctype>
#include <cstring>

using namespace std;

const int MAX_N = 410;
const int mod = 1e9 + 7;

int read();

int a[MAX_N][MAX_N];

long long f[MAX_N][MAX_N];

int check(int x1, int x2, int y1, int y2) {
    return a[x2][y2] - a[x1 - 1][y2] - a[x2][y1 - 1] + a[x1 - 1][y1 - 1];   
}

long long DFS(int l, int r) {
    
    if (l >= r)    return 1;
    if (~f[l][r])  return f[l][r];
    
    long long res = 0;  
    for (int i = l; i <= r; ++i) {
        if (check(l, l, l + 1, i) || check(i + 1, r, l, i))  continue;
        res = (res + DFS(l + 1, i) * DFS(i + 1, r) % mod) % mod;
    }
    
    return f[l][r] = res;
}

int main() {
    
    int T = read();
    
    while (T--) {
        int N = read(), M = read(); 
        
        memset(a, 0, sizeof a);
        memset(f, -1, sizeof f);
        
        for (int i = 1; i <= M; ++i) {
            int u = read(), v = read();
            a[u][v]++;  
        }
        
        for (int i = 1; i <= N; ++i)
            for (int j = 1; j <= N; ++j)
                a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
        
        printf("%lld\n", DFS(1, N));
    }
    
}

int read() {
    int x = 0, c = getchar();
    while (!isdigit(c))  c = getchar();
    while (isdigit(c))   x = x * 10 + c - 48, c = getchar();
    return x;
}

Guess you like

Origin www.cnblogs.com/VeniVidiVici/p/11616220.html