Prototypes analyze(二叉排序树,不同树形个数)

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;
}

时间: 2024-11-05 03:47:05

Prototypes analyze(二叉排序树,不同树形个数)的相关文章

nyoj 1278G: Prototypes analyze 与 二叉排序树(BST)模板

参考博客:https://blog.csdn.net/stpeace/article/details/9067029 参考博客:https://blog.csdn.net/baidu_35643793/article/details/70792326 先放上二叉搜索树的板子 #include <iostream> using namespace std; // BST的结点 typedef struct node { int key; struct node *lChild, *rChild;

2016河南省第九届ACM程序设计竞赛【正式赛真题】

A题:表达式求值 时间限制:1000 ms  |  内存限制:65535 KB 描述 假设表达式定义为:1. 一个十进制的正整数 X 是一个表达式.2. 如果 X 和 Y 是 表达式,则 X+Y, X*Y 也是表达式; *优先级高于+.3. 如果 X 和 Y 是 表达式,则 函数 Smax(X,Y)也是表达式,其值为:先分别求出 X ,Y值的各位数字之和,再从中选最大数.4.如果 X 是 表达式,则 (X)也是表达式.例如:表达式 12*(2+3)+Smax(333,220+280) 的值为 6

每天一套题打卡|河南省第九届ACM/ICPC

A 表达式求值 表达式求值:可以用递归求解,也可以用栈模拟,考过多次. 类似题目:NYOJ305,NYOJ35 用栈模拟做法: #include <stdio.h> #include <string.h> #include <stack> using namespace std; stack<int> dsta;//数据栈 stack<char> osta;//字符栈 char s[1005]; int main() { int T,i,j; s

算法设计与分析 - 李春葆 - 第二版 - pdf-&gt;word v3

1 1.1 第1章─概论 2 3 1.1.1 练习题 4 1. 下列关于算法的说法中正确的有( ). 5 Ⅰ.求解某一类问题的算法是唯一的 6 Ⅱ.算法必须在有限步操作之后停止 7 Ⅲ.算法的每一步操作必须是明确的,不能有歧义或含义模糊 8 Ⅳ.算法执行后一定产生确定的结果 9 A. 1个 B.2个 C.3个 D.4个 10 2. T(n)表示当输入规模为n时的算法效率,以下算法效率最优的是( ). 11 A.T(n)= T(n-1)+1,T(1)=1 B.T(n)= 2n2 12 C.T(n)

【LeetCode】96. Unique Binary Search Trees-唯一二叉排序树的个数

一.描述: 二.思路: BST(二叉排序树):中序遍历的结果为非递减序列,并且节点(个数和值)相同的不同二叉树的中序遍历结果都相同: 当左子树的节点个数确定后,右子树的个数也随之确定: 当节点个数为0或1时,二叉树只有1种,表示为f(0)=1,f(1)=f(0)*f(0): 当节点个数为2时,总的种类数=左子树为空f(0)*右子树不为空f(1)+左子树不为空f(1)*右子树为空f(0),即f(0)*f(1)+f(1)*f(0)=2种: 当节点个数为3时,有左子树为空f(0)*右子树不为空f(2)

hdu5593--ZYB&#39;s Tree(树形dp)

问题描述 ZYB有一颗N个节点的树,现在他希望你对于每一个点,求出离每个点距离不超过KK的点的个数. 两个点(x,y)在树上的距离定义为两个点树上最短路径经过的边数, 为了节约读入和输出的时间,我们采用如下方式进行读入输出: 读入:读入两个数A,B,令fai??为节点i的父亲,fa?1??=0;fa?i??=(A∗i+B)%(i−1)+1,i∈[2,N] . 输出:输出时只需输出N个点的答案的xor和即可. 输入描述 第一行一个整数TT表示数据组数. 接下来每组数据: 一行四个正整数N,K,A,

Codeforces 462D Appleman and Tree 树形dp

题目链接:点击打开链接 题意: 给定n个点的树, 0为根,下面n-1行表示每个点的父节点 最后一行n个数 表示每个点的颜色,0为白色,1为黑色. 把树分成若干个联通块使得每个联通块有且仅有一个黑点,问有多少种分法(结果mod1e9+7) 思路: 树形dp,每个点有2个状态,已经归属于某个黑点和未归属于某个黑点. #include <cstdio> #include <vector> #include <iostream> using namespace std; #de

选课 树形DP+多叉树转二叉树+dfs求解答案

问题 A: 选课 时间限制: 1 Sec  内存限制: 128 MB提交: 6  解决: 3[提交][状态][答疑][寄存][题解] 题目描述 大 学里实行学分.每门课程都有一定的学分,学生只要选修了这门课并考核通过就能获得相应的学分.学生最后的学分是他选修的各门课的学分的总和. 每个学生都要选择规定数量的课程.其中有些课程可以直接选修,有些课程需要一定的基础知识,必须在选了其它的一些课程的基础上才能选修.例如,<数据结 构>必须在选修了<高级语言程序设计>之后才能选修.我们称&l

hzau 1201 Friends(树形dp)

1201: Friends Time Limit: 1 Sec  Memory Limit: 1280 MBSubmit: 119  Solved: 25[Submit][Status][Web Board] Description In a country, the relationship between people can be indicated by a tree. If two people are acquainted with each other, there will be