【题解】1929: Prototypes analyze⭐⭐⭐ 【二叉搜索树】

博客围绕天花板原型分析展开,将各层抗塌陷值按规则插入二叉搜索树,旨在分析树形状与建筑质量的关联。给定一组原型,需确定其诱导出的不同树形状数量。题解强调关注树的结构,通过构建BST并查找来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1929: Prototypes analyze

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.

Input

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.

Output

For each test case generate a single line containing a single integer that is the number of different tree
shapes.

Examples

样例输入 Copy
1
5 3
2 7 1
1 5 9
3 1 4
2 6 5
9 7 3
样例输出 Copy
4

Hint




题意:

给出N组数, 每组k个数, 对于每组数顺序插入勾践二叉搜索树.
问不同形状二叉搜索树有几种.

题解:

注意, 考虑的是形状 ! 而不是具体的数, 所以我们要重点关心结构.
对于每组数, 我们都构建BST, 然后查找一下就好了, 其实不难.

经验小结:

要时刻牢记, 你要关心的, 到底是什么
学习一下BST的使用方法


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const int inf = 1<<30;
const LL maxn = 110;
 
struct node{
    int v;
    node *l, *r;
    node(int vv, node *ll, node *rr){v = vv, l = ll, r = rr;}
}*T[maxn];
node *Insert(node *p, int v){
    if(p==NULL){
        node *q = new node(v, NULL, NULL);
        return q;
    }
    if(p->v == v) return p;
    else if(v < p->v) p->l = Insert(p->l, v);
    else p->r = Insert(p->r, v);
    return p;
}
bool judge(node *t1, node *t2){
    if(t1==NULL&&t2==NULL)
        return true;
    else if(t1 && t2)   //重点在于判断结构
        return judge(t1->l, t2->l)&&judge(t1->r, t2->r);
    else
        return false;
}
int main()
{
    int t, n, m, v;
    cin >> t;
    while(t--){
        ms(T, 0);
        cin >> n >> m;
        for(int i = 1; i <= n; i++){
            node *root = NULL;
            for(int j = 1; j <= m; j++){
                cin >> v;
                root = Insert(root, v);   //使用方法
            }
            T[i] = root;
        }
 
        int ans = 0;
        for(int i = 1; i <= n; i++){
            bool flag = true;
            for(int j = i+1; j <= n; j++)
                if(judge(T[i], T[j])){
                    flag = false;
                    break;
                }
            if(flag) ++ans;
        }
        cout << ans << endl;
    }
 
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值