题目描述:
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 1e6, 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 <bits/stdc++.h>
using namespace std;
const int N=55;
struct node{
node *left,*right;
int w;
node(int ww):w(ww),left(NULL),right(NULL){}
node(){}
}*tree[N];
int n,k,tmpw;
int vis[N];
node* build(node *root,int w){//建树
if(root==NULL){
return new node(w);
}
if(w>root->w) root->right=build(root->right,w);
else root->left=build(root->left,w);
return root;
}
bool judge(node *t1,node *t2){//判断
if(!t1&&!t2){
return true;
}
else if(t1!=NULL&&t2!=NULL){
return judge(t1->left,t2->left)&&judge(t1->right,t2->right);
}
return false;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(vis,0,sizeof(vis));
scanf("%d %d",&n,&k);
for(int i=0;i<n;i++){
tree[i]=NULL;
}
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
scanf("%d",&tmpw);
tree[i]=build(tree[i],tmpw);
}
}
int cnt=0;
for(int i=0;i<n;i++){
if(vis[i]) continue;
cnt++;
for(int j=i+1;j<n;j++){
if(judge(tree[i],tree[j])){
vis[j]=1;
}
}
}
printf("%d\n",cnt);
}
return 0;
}