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 ...
随机推荐
- 通过WebRTC实现实时视频通信(二)
通过WebRTC实现实时视频通信(一) 通过WebRTC实现实时视频通信(二) 通过WebRTC实现实时视频通信(三) 在上一篇文章中,我们讲解了WebRTC的概述.历史.安全性和开发者工具.接下来我 ...
- ufldl学习笔记与编程作业:Softmax Regression(vectorization加速)
ufldl学习笔记与编程作业:Softmax Regression(vectorization加速) ufldl出了新教程,感觉比之前的好.从基础讲起.系统清晰,又有编程实践. 在deep learn ...
- Android Studio怎样加入工程(project)为library(针对非gradle)
这篇文章还是针对非gradle build的project,gradle build有一些区别.在Eclipse要引用别的project为本project的library非常easy,可是在Andro ...
- MISRA-C++ 2008
- java面试第十四天
包名.类名和属性可以被序列化,方法和构造器不会被序列化的. 静态属性不会被序列化的. 属性会被递归序列化的,也就是一个类中有引用类型的属性,如果这个属性对应的类实现了Serializable接口,在对 ...
- webpack配置上线地址
webpack配置上线地址主要使用output配置项下的publicPath. webpack.config.js配置文件为: var htmlWebpackPlugin = require('htm ...
- 传统数据库没落,OLTP新型数据库发展火热
參考资料: (1) <OLTP Through the Looking Glass, and What We Found There> (2) <The End of an Arch ...
- SQLite的升级(转)
做Android应用,不可避免的会与SQLite打交道.随着应用的不断升级,原有的数据库结构可能已经不再适应新的功能,这时候,就需要对SQLite数据库的结构进行升级了. SQLite提供了ALTER ...
- HTTP Header具体解释
HTTP(HyperTextTransferProtocol) 即超文本传输协议,眼下网页传输的的通用协议.HTTP协议採用了请求/响应模型,浏览器或其它client发出请求.server给与响应.就 ...
- iOS最为简单时间轴(GZTimeLine)
概述 迄今为止最为简单的时间轴 :可以自定义(类似于美团的送餐信息) 详细 代码下载:http://www.demodashi.com/demo/10797.html 迄今为止 最为简单的时间轴 :可 ...