UVA 1264 - Binary Search Tree(BST+计数)
UVA 1264 - Binary Search Tree
题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列)
思路:先建树,然后dfs一遍,对于一个子树而言,仅仅要保证左边和右边顺序对就能够了,所以种数为C(左右结点总数,左结点),然后依据乘法原理乘上左右子树的情况就可以
代码:
#include <cstdio>
#include <cstring> typedef long long ll; const int MAXNODE = 1111111; const int N = 21;
const int MOD = 9999991; int C[N][N]; struct BST {
struct Node {
int l, r, val, lsz, rsz;
Node() {l = 0, r = 0, val = -1; lsz = 0; rsz = 0;}
} node[MAXNODE]; int sz; void init() {
node[1] = Node();
sz = 2;
} void insert(int x, int v) {
if (node[x].val == -1) {
node[x].val = v;
return;
}
if (v < node[x].val) {
if (!node[x].l) {
node[sz] = Node();
node[x].l = sz++;
}
insert(node[x].l, v);
node[x].lsz++;
}
else {
if (!node[x].r) {
node[sz] = Node();
node[x].r = sz++;
}
insert(node[x].r, v);
node[x].rsz++;
}
} int dfs(int x) {
if (x == 0) return 1;
return (ll)dfs(node[x].l) * dfs(node[x].r) % MOD * C[node[x].lsz + node[x].rsz][node[x].lsz] % MOD;
} void solve() {
init();
int n, num;
scanf("%d", &n);
while (n--) {
scanf("%d", &num);
insert(1, num);
}
printf("%d\n", dfs(1));
} } gao; int t; void getC() {
for (int i = 0; i < N; i++) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; j++)
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
} int main() {
getC();
scanf("%d", &t);
while (t--) {
gao.solve();
}
return 0;
}
UVA 1264 - Binary Search Tree(BST+计数)的更多相关文章
- Lowest Common Ancestor of a Binary Search Tree (BST)
Given a binary search tree(BST), find the lowest common ancestor of two given nodes in the BST. Node ...
- PAT 1099 Build A Binary Search Tree[BST性质]
1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...
- Convert Sorted List to Balanced Binary Search Tree (BST)
(http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html) Given a singly linked list ...
- Convert Sorted Array to Balanced Binary Search Tree (BST)
(http://leetcode.com/2010/11/convert-sorted-array-into-balanced.html) Given an array where elements ...
- Convert Binary Search Tree (BST) to Sorted Doubly-Linked List
(http://leetcode.com/2010/11/convert-binary-search-tree-bst-to.html) Convert a BST to a sorted circu ...
- Binary Search Tree BST Template
Use one queue + size variable public class Solution { public ArrayList<ArrayList<Integer>&g ...
- 【题解】【BST】【Leetcode】Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)
题目描述: 一个二叉搜索树,给定两个节点a,b,求最小的公共祖先 _______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5 例如: 2,8 - ...
- (BST 递归) leetcode98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
随机推荐
- 在Foreda上安装apache-tomcat-7.0.42.tar.gz
开发环境JDK和Tomcat应该和部署环境一致,要不容易出现奇奇怪怪的问题.所以Aspire机器上的Tomcat要装一个新版本了. 装Tomcat基本等于一个解压和移动的过程,确实简单. 第一步:解压 ...
- Linux:编译动态库时遇到的错误relocation R_X86_64_32 against `a local symbol'
编译动态库时遇到如下错误: ... ... relocation R_X86_64_32 against `a local symbol' can not be used when making a ...
- linux cp覆盖每次都有提示
1.cp命令,目标已经存在,每次都提示是否覆盖,怎么办? 2.cp --help 可以看到选项-i的时候,才会提示,但是这里并没有-i,为什么每次都有提示? 3.原因是:这里执行的cp是一个别名,通过 ...
- [android错误] Installation error: INSTALL_FAILED_VERSION_DOWNGRA
错误表现: [2014-06-27 18:19:51 - XXX] Installing XXXX.apk... [2014-06-27 18:20:00 - XXX] Installation er ...
- 关于oracle redo log buffer 你所不知道的东西
[ora11@lixora ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.1.0 Production on Wed Oct 8 09:57:50 ...
- 用Main方法调用freemarker生成文件
MyGenerator.java package com.comp.common; import java.io.BufferedWriter; import java.io.File; import ...
- ZH奶酪:PHP遍历目录/文件的3种方法
其实PHP中内建函数scandir()就可以返回目录下全部文件和目录了... ========================== 1.使用$obj = dir($dir)返回目录对象$obj,然后使 ...
- 在LinuxMint中对firefox进行手动安装flash插件
/********************************************************************* * Author : Samson * Date ...
- Docker网络一览
转自:http://dockone.io/article/1143 [编者的话]本文是Nuage Networks公司Filip Verloy的一篇博文,简介了一下Docker网络情况,单主机的四种模 ...
- URAL 题目1297. Palindrome(后缀数组+RMQ求最长回文子串)
1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just ...