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)) 二分 ...
随机推荐
- HTML 5 视频使用
视频格式 当前,video 元素支持三种视频格式: 格式 IE Firefox Opera Chrome Safari Ogg No 3.5+ 10.5+ 5.0+ No MPEG 4 9.0+ No ...
- windows服务器性能监控工具、方法及关键指标
原文:http://www.cnblogs.com/liulun/p/3543777.html 监控方法 推荐使用windows自带的“性能监视器”(老版本的windows叫性能计数器)来监控服务器的 ...
- <经验杂谈>C#中一种最简单、最基本的反射(Reflection):通过反射获取方法函数
说起反射之前和很多用C#/.net的同仁们一样,相比于一般应用层对数据的增删改查总有点觉得深奥到难以理解.其实程序这东西,用过.实践过就很简单,我一直这么认为. 先说下概念:反射 Reflection ...
- 不安装开发环境,查看logcat日志
#公司的测试终于想到要看 android 的日志.安装开发环境太麻烦了,这里有个简单的办法. 我把\sdk\platform-tools目录中,文件名以"adb"开头的三个文件打包 ...
- ORACLE SQL单行函数(一)【weber出品必属精品】
1.SUBSTR:求父串中的子串 SUBSTR('HelloWorld',1,5) 1:代表子串的起始位置,如果为正,正数,如果为负,倒数 5:代表字串的终止位置,只能向右数,可以省略,如果省略就是数 ...
- 解决mdi窗体闪烁的问题
/// 解决mdi窗体闪烁的问题 /// </summary> protected override CreateParams CreateParams { get { CreatePar ...
- C#this的五种用法
this的五种用法: 1.使用被掩盖的成员变量: class AA { int a; public void set1(int a) { this.a = a;//right } public voi ...
- hdu 1282 回文数猜想
Problem Description 一个正整数,如果从左向右读(称之为正序数)和从右向左读(称之为倒序数)是一样的,这样的数就叫回文数.任取一个正整数,如果不是回文数,将该数与他的倒序数相加,若其 ...
- [Leetcode] Merge Sorted Array (C++)
我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目: Gi ...
- web app之rem
rem是什么? rem:font size of the root element,是指相对于根元素的字体大小的单位.简单的说它就是一个相对单位. em:font size of the elemen ...