Prototypes analyze(二叉排序树,不同树形个数)
2016-06-15 21:23
513 查看
Prototypes analyze
时间限制:1000 ms | 内存限制:65535 KB难度:2
描述
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[55]; 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[55]; while(T--){ int v; scanf("%d%d", &n, &k); for(int i = 0; i < n; i++){ Tree p = NULL; for(int j = 0; j < k; j++){ scanf("%d", &v); build(p, v); } tree[i] = p; } int ans = 0; for(int i = 0; i < n; i++){ int flot = 1; for(int j = i + 1; j < n; j++){ jud = true; judge(tree[i], tree[j]); if(jud){ flot = 0; break; } } ans += flot; } printf("%d\n", ans); } return 0; }
相关文章推荐
- 安全防范:服务器连接及权限处理
- 仿win8磁贴界面以及功能
- HttpURLConnection的用法
- 智能指针的实现
- 什么是Activity,详细介绍Activity
- 【GDOI2016模拟3.15】染色
- C++常识之——C++中堆和栈的区别,自由存储区、全局/静态存储区和常量存储区(转)
- 如何压缩Golang 编译出的可执行文件大小
- STM32 UVC学习笔记2
- javac -d . hello.java中的 -d .是什么作用?
- __FILE__,__LINE__,__func__ 真好用,DEBUG利器啊!
- 【复习】servlet之Http状态码
- MSSQL中全局变量@@identity与方法scope_identity()的区别
- Codeforces Round #357 (Div. 2)
- NYOJ疯牛【二分】
- Redis进阶
- Python类的特点 (3) :静态方法与类方法
- 机器设备(dfs)
- 字符串
- 产品经理必备成长修炼秘籍(深度解析)