题目:

1013: Prototypes analyze

时间限制: 1 Sec  内存限制: 128 MB
提交: 6  解决: 4
[提交][状态][讨论版]

题目描述

ALpha Ceiling Manufacturers (ACM) is analyzing the p roperties 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.
*T he next n lines describe the ceiling prototypes. Each of these lines contains k distinct
integers ( between 1 and 10 6 , 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
大意就是告诉你一种构造二叉树的方法和每颗二叉树所含的元素值,给出若干颗二棵树,判断有几种不同样式的二叉树;
第一次知道,指针+结构体构造二叉树,所以1A时用的是一维数组耗费了大量的内存;
代码:

#include<iostream>
#include<cstring>
using namespace std;
typedef struct node
{
node *l,*r;
int v;
}*Tree;                      //Tree类型表示node型指针(Tree a==node *a)
bool pd=true;
Tree tree[55];

void build(Tree &p,int v)        //用引用直接改变p的树形形态,不然的话会构造失败
{
if(p==NULL) {
p=new node();
p->v=v;
p->l=NULL;
p->r=NULL;
}
else if(v<p->v) build(p->l,v);
else if(v>p->v) build(p->r,v);
}
void judge(Tree a,Tree b)
{
if(a==NULL&&b==NULL) return;
else if(a&&b){
judge(a->r,b->r);
judge(a->l,b->l);
}
else pd=false; //不相等                       false表示两棵树形状不相同
}

int main()
{
int t,i,j,n,m,k;
int a;
cin>>t;
while(t--){int ans=0;
cin>>n>>k;
for(j=1;j<=n;++j){
Tree p=NULL;
for(i=1;i<=k;++i){
cin>>a;
build(p,a);
}
tree[j]=p;
}
for(i=1;i<=n;++i){
for(j=i+1;j<=n;++j){
pd=true;                                           //先假设两棵树相等
judge(tree[i],tree[j]);
if(pd) break;                                      //pd值不变说明两棵树真的相等,出现重复直接break
}
if(!pd) ++ans;                                  //比较到最后pd还是false说明不重复+1
}
cout<<ans<<endl;
}
return 0;
}

二叉树—-1(No.9HN省赛小题)的更多相关文章

  1. Python解答蓝桥杯省赛真题之从入门到真题(二刷题目一直更新)

    蓝桥刷题 原文链接: https://github.com/libo-sober/LanQiaoCup Python解答蓝桥杯省赛真题之从入门到真题 不同字串 """ 一 ...

  2. 2013年山东省赛F题 Mountain Subsequences

    2013年山东省赛F题 Mountain Subsequences先说n^2做法,从第1个,(假设当前是第i个)到第i-1个位置上哪些比第i位的小,那也就意味着a[i]可以接在它后面,f1[i]表示从 ...

  3. HEX SDUT 3896 17年山东省赛D题

    HEX SDUT 3896 17年山东省赛D题这个题是从矩形的左下角走到右上角的方案数的变形题,看来我对以前做过的题理解还不是太深,或者是忘了.对于这种题目,直接分析它的性质就完事了.从(1,1)走到 ...

  4. 第十届蓝桥杯JavaB组省赛真题

    试题 A: 组队 本题总分:5 分 [问题描述] 作为篮球队教练,你需要从以下名单中选出 1 号位至 5 号位各一名球员, 组成球队的首发阵容. 每位球员担任 1 号位至 5 号位时的评分如下表所示. ...

  5. 常让人误解的一道js小题

    一道小题引发的深思 今天无意中看到一个js笔试题,不由得想起初学js那会被各种题目狂虐的心酸,虽说现在也会被笔试题所虐,但毕竟比之前好了很多,下面就是我的个人理解,欢迎拍砖.指正: var x = 1 ...

  6. 一些js小题(一)

    一些js小题,掌握这些对于一些常见的面试.笔试题应该很有帮助: var a=10; function aa(){ alert(a); } function bb(){ aa(); } bb();//1 ...

  7. 2013杭州现场赛B题-Rabbit Kingdom

    杭州现场赛的题.BFS+DFS #include <iostream> #include<cstdio> #include<cstring> #define inf ...

  8. 关于理解python类的小题

    今天看了python部落翻译的一篇<一道python类的小题>文章,感觉挺有启发性,记录下来: print('A') class Person(object): print('B') de ...

  9. 2017年第六届数学中国数学建模国际赛(小美赛)C题解题思路

    这篇文章主要是介绍下C题的解题思路,首先我们对这道C题进行一个整体的概括,结构如下: C题:经济类 第一问:发现危险人群. 发现:欺诈的方式开始.雇佣或浪漫的承诺. 数据→确定特定的经济萧条地区→确定 ...

随机推荐

  1. php CI框架实现验证码功能和增强验证码安全性实战教程

    php CI框架实现验证码功能和增强验证码安全性实战教程 CodeIgniter简称CI是最流行的一个php MVC框架之一,本人讲从实际项目使用中写系列实战经验,有别与其他的理论讲解文章,会附上实战 ...

  2. JMeter:全面的乱码解决方案

    中文乱码一直都是比较让人棘手的问题,我们在使用Jmeter的过程中,也会遇到中文乱码问题 接口:http://127.0.0.1:8090/test 这个接口有一个参数name,返回结果就是你传的na ...

  3. 我用了7年时间成长为阿里Java架构师,你呢?(附学习路线图)

      前言:我用了七年的时间,一步一步走到了现在,中途也有了解过其他的技术,也想过要转其他的语言,但是最后还是坚持下来走Java这条路,希望我的经历可以帮助到后来的人,要是觉得对你有帮助的话,可以点赞关 ...

  4. P2P原理及UDP穿透简单说明

    转:http://http://andylin02.iteye.com/blog/444666 P2P原理及UDP穿透简单说明 本文章出自cnntec.com的AZ猫著,如需要转发,请注明来自cnnt ...

  5. Django框架介绍之cookie与session

    cookie http请求时无状态的,一个客户端第一次,第二次,第n次访问同一个服务器都是一样的,服务器都会按照一个新的连接处理.但是,有时候客户端需要服务器记住客户端的登录状态,譬如离开一会,回来之 ...

  6. bzoj1643 / P2666 [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪

    [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪 简单的dfs题 枚举前3个完全平方数,判断最后一个是不是完全平方数,统计合法方案数即可. (zz选手竟把数 ...

  7. Python3 Selenium定位不到元素常见原因及解决办法

    Python3 Selenium定位不到元素常见原因及解决办法 一.问题描述 在做web应用的自动化测试时,定位元素是必不可少的,这个过程经常会碰到定位不到元素的情况: 报错信息: no such e ...

  8. HTML5 多媒体音视频处理

    HTML5 多媒体音视频处理 版权声明:未经博主授权,内容严禁转载 ! 音频处理 - audio HTML5 Audio 音频 目前大多数音频是通过哦插件的形式来播放的. 不同浏览器在网页上播放音频的 ...

  9. OpenCV相关网站推荐(Informative websites related to OpenCV)

    原文来自:http://answers.opencv.org/question/69691/informative-websites-related-to-opencv/ i think it wil ...

  10. VC++ 实现修改文件创建、访问、修改时间属性(转载)

    转载:http://sunnysab.blog.163.com/blog/static/18037500920134221295425/ struct _FILETIME { //结构体定义 DWOR ...