Prototypes analyze(二叉排序树,不同树形个数)
Prototypes analyze
- 描述
-
ALpha Ceiling Manufacturers (ACM) is analyzing the properties of its new series of Incredibly Collapse-Proof Ceilings (ICPCs). An ICPC consists of n layers of material, each with a different value of collapse resistance (measured as a positive integer). The analysis ACM wants to run will take the collapse-resistance values of the layers, store them in a binary search tree, and check whether the shape of this tree in any way correlates with the quality of the whole construction. Because, well, why should it not? To be precise, ACM takes the collapse-resistance values for the layers, ordered from the top layer to the bottom layer, and inserts them one-by-one into a tree. The rules for inserting a value v are: • If the tree is empty, make v the root of the tree. • If the tree is not empty, compare v with the root of the tree. If v is smaller, insert v into the left subtree of the root, otherwise insert v into the right subtree. ACM has a set of ceiling prototypes it wants to analyze by trying to collapse them. It wants to take each group of ceiling prototypes that have trees of the same shape and analyze them together. For example , assume ACM is considering five ceiling prototypes with three layers each, as described by Sample Input 1 and shown in Figure C.1. Notice that the first prototype’s top layer has collapseresistance value 2, the middle layer has value 7, and the bottom layer has value 1. The second prototype has layers with collapse-resistance values of 3, 1, and 4 – and yet these two prototypes induce the same tree shape, so ACM will analyze them together. Given a set of prototypes, your task is to determine how many different tree shapes they induce.
- 输入
- The first line of the input contains one integers T, which is the nember of test cases (1<=T<=8). Each test case specifies: * Line 1: two integers n (1 ≤ n ≤ 50), which is the number of ceiling prototypes to analyze, and k (1 ≤ k ≤ 20), which is the number of layers in each of the prototypes. *The next n lines describe the ceiling prototypes. Each of these lines contains k distinct integers ( between 1 and 106 , inclusive ) , which are the collapse-resistance values of the layers in a ceiling prototype, ordered from top to bottom.
- 输出
- For each test case generate a single line containing a single integer that is the number of different tree shapes.
- 样例输入
-
1
5 3
2 7 1
1 5 9
3 1 4
2 6 5
9 7 3 - 样例输出
-
4
- 来源
- 河南省第九届省赛
- 题解:其实也挺水的,赛场上一直用的深度左右树来存的数形,其实只要把树存起来不就好了。。。当时脑子抽住了。。。
- 代码:
-
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
using namespace std; typedef struct Node{
Node *l, *r;
int v;
}Node, *Tree; void build(Tree &p, int v){
if(p == NULL){
p = new Node();
p->v = v;
p->l = p->r = NULL;
return ;
}
if(v < p->v){
build(p->l, v);
}
else if(v > p->v){
build(p->r, v);
}
}
Tree tree[];
void visit(Tree p){ if(p->l){
visit(p->l);
}
if(p->r){
visit(p->r);
}
}
bool jud;
bool judge(Tree a, Tree b){
if(a == NULL && b == NULL)return true;
else if(a && b){
judge(a->l, b->l);
judge(a->r, b->r);
}
else
jud = false;
}
int main(){
int T, n, k;
scanf("%d", &T);
string s[];
while(T--){
int v;
scanf("%d%d", &n, &k);
for(int i = ; i < n; i++){
Tree p = NULL;
for(int j = ; j < k; j++){
scanf("%d", &v);
build(p, v);
}
tree[i] = p;
}
int ans = ;
for(int i = ; i < n; i++){
int flot = ;
for(int j = i + ; j < n; j++){
jud = true;
judge(tree[i], tree[j]);
if(jud){
flot = ;
break;
}
}
ans += flot;
}
printf("%d\n", ans);
}
return ;
}
Prototypes analyze(二叉排序树,不同树形个数)的更多相关文章
- nyoj 1278G: Prototypes analyze 与 二叉排序树(BST)模板
参考博客:https://blog.csdn.net/stpeace/article/details/9067029 参考博客:https://blog.csdn.net/baidu_35643793 ...
- 二叉树—-1(No.9HN省赛小题)
题目: 1013: Prototypes analyze 时间限制: 1 Sec 内存限制: 128 MB提交: 6 解决: 4[提交][状态][讨论版] 题目描述 ALpha Ceiling M ...
- 每天一套题打卡|河南省第九届ACM/ICPC
A 表达式求值 表达式求值:可以用递归求解,也可以用栈模拟,考过多次. 类似题目:NYOJ305,NYOJ35 用栈模拟做法: #include <stdio.h> #include &l ...
- 记忆化搜索 codevs 2241 排序二叉树
codevs 2241 排序二叉树 ★ 输入文件:bstree.in 输出文件:bstree.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 一个边长为n的正三 ...
- 算法设计与分析 - 李春葆 - 第二版 - html v2
1 .1 第 1 章─概论 1.1.1 练习题 1 . 下列关于算法的说法中正确的有( ). Ⅰ Ⅱ Ⅲ Ⅳ .求解某一类问题的算法是唯一的 .算法必须在有限步操作之后停止 .算法 ...
- nyoj-1278-Prototypes analyze(二叉排序树模板)
题目链接 思路:建树之后,判断有多少种不同的树. 判断不同的树,简单的思路是遍历数组,判断数组后面是否存在一样的树 /* Name:NYOJ-1278-Prototypes analyze Copyr ...
- UVALive-8072 Keeping On Track 树形dp 联通块之间缺失边的个数
题目链接:https://cn.vjudge.net/problem/UVALive-8072 题意 给出n+1个点和n条边,每对点之间只能存在一条边. 现在要找出一个节点,使得去掉这个点后,所剩每对 ...
- 012-数据结构-树形结构-哈希树[hashtree]、字典树[trietree]、后缀树
一.哈希树概述 1.1..其他树背景 二叉排序树,平衡二叉树,红黑树等二叉排序树.在大数据量时树高很深,我们不断向下找寻值时会比较很多次.二叉排序树自身是有顺序结构的,每个结点除最小结点和最大结点外都 ...
- 【数据结构】简单谈一谈二分法和二叉排序树BST查找的比较
二分法查找: 『在有序数组的基础上通过折半方法不断缩小查找范围,直至命中或者查询失败.』 二分法的存储要求:要求顺序存储,以便于根据下标随机访问 二分法的时间效率:O(Log(n)) 二分 ...
随机推荐
- AIX下RAC搭建 Oracle10G(一)检測系统环境
AIX下RAC搭建系列 环境 节点 节点1 节点2 小机型号 IBM P-series 630 IBM P-series 630 主机名 AIX203 AIX204 交换机 SAN光纤交换机 存储 S ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- dataset导出成excel
之前网上查找了很多关于这类的代码.要不是中文乱码,要不是就是太复杂.这个是我用过最好用的. //ds为数据源,filename为保存的文件名 publicvoidCreateExcel(DataSet ...
- WebApi2官网学习记录--HTTP Message Handlers
Message Handlers是一个接收HTTP Request返回HTTP Response的类,继承自HttpMessageHandler 通常,一些列的message handler被链接到一 ...
- Oracle查找重复数据
Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)
- oracle时间戳转换
select (to_date('2013-04-09 14:02:15','yyyy-mm-dd hh24:mi:ss') - to_date('1970-01-01','yyyy-mm-dd')) ...
- 分享一个通用的分页SQL
又很久没写博客,今天记录一个SQLserver通用分页存储过程(适用于SqlServer2000及以上版本) 1.支持连表 2.支持条件查询 USE [MYDB] GO /****** Object: ...
- 查看kafka的group.id
kafka/config目录下的consumer.properties中可以看到
- 此项目的默认Web访问模式设置为文件共享, 但是无法从路径(此为转贴)
故障现象: 当你打开ASP.NET Web项目时,如果出现这样的错误提示:提示窗口标题: Web访问失败提示内容: 此项目的默认Web访问模式设置为文件共享, 但是无法从路径“...”打开“...”处 ...
- Shell脚本中单引号(‘)和双引号(“)的使用区别[转载]
shell可以识别4种不同类型的引字符号: 单引号字符' 双引号字符" 反斜杠字符\ 反引号字符` 1. 单引号 ( '' ) # grep Susan phonebook Susan Go ...