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)) 二分 ...
随机推荐
- 奔五的人学IOS:swift练手与csdn,最近学习总结
早在五月份就准备開始学习ios开发,当时还是oc,学习了几天,最终不得其法.到了ios8开放,再加swift的出现.从10月份開始.最终找到了一些技巧,学习起来还算略有心得. 今天把我在学习swift ...
- 用消息在Win32控制台程序多线程间进行通讯
#include <stdio.h> #include <windows.h> //#include <iostream> //#include <pro ...
- <经验杂谈>查询表结构的SQL语句
在我们使用SQL数据库的过程中,经常会遇到查询表结构的情况,以下就是sql语句的写法: --查询非系统数据库 SELECT name FROM Master..SysDatabases 查询数据库下所 ...
- CentOS管理
1.使用yum安装和卸载软件 主要功能是更方便的添加/删除/更新RPM包. 它能自动解决包的倚赖性问题. 它能便于管理大量系统的更新问题 一.yum list|more 列 ...
- Socket学习笔记
..........(此处略去万万字)学习中曲折的过程不介绍了,直接说结果 我的学习方法,问自己三个问题,学习过程将围绕这三个问题进行 what:socket是什么 why:为什么要使用socket ...
- mongodb 详解 error:10061 由于目标计算机积极拒绝,无法连接解决方法
mongodb下载地址(32位):下载地址 自己选择版本 建立如下与mongodb并行的两个文件夹data和log. 然后建立mongo.config. 在mongo.config配置文件中输入: # ...
- (转)单例模式(Singleton)
首先来明确一个问题,那就是在某些情况下,有些对象,我们只需要一个就可以了, 比如,一台计算机上可以连好几个打印机,但是这个计算机上的打印程序只能有一个, 这里就可以通过单例模式来避免两个打印作业同时输 ...
- 修饰器模式(day04)
修饰器设计模式 --最近我给女朋友买了一款可以更换外壳的手机.现在的外壳是红色的,假如我想用这款手机的时候,会更换成银灰色的外壳.但是我不能随意更换天线或者话筒,因为这些功能模块在手机生产的时候就已经 ...
- ecshop中404错误页面设置
在ecshop系统当中,比如你随意将商品详细页面的地址中的ID修改为一个不存在的商品ID,ecshop会自动跳转到首页.ecshop在这方面做得非常的差,甚至导致了很多的站不被搜索引擎收录.最模板提供 ...
- Spring + Tomcat 启动报错java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool
错误如下: -- ::,-[TS] INFO http-- org.springframework.beans.factory.support.DefaultListableBeanFactory - ...