NYOJ - [第九届河南省程序设计大赛]Prototypes analyze(二叉树)

题目链接:http://nyoj.top/problem/1278

  • 内存限制:64MB 时间限制:1000ms 

题目描述

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

解题思路

构建二叉树,判断构建的二叉树有几种形状。我们可以利用数组构建一个二叉树,然后判断结点是否存在,存在为1,否则为零,最后判断一下有多少不同的01串。这种方法简单暴力,只不过浪费时空。另外一种方法就是利用链表构建二叉树,然后在遍历此二叉树,将编号存下来,最后找不同的有多少就行了。
法一:

#include <bits/stdc++.h>
using namespace std;
int tree[1100000], max_;
char s[52][1050000];
void Create(int root, int e) {
    if (!tree[root]) {
        max_ = max(root, max_);
        tree[root] = e;
    }
    else {
        if (e < tree[root])
            Create(root * 2, e);
        else Create(root * 2 + 1, e);
    }
}
int main() {
    int t, n, k, e;
    scanf("%d", &t);
    while (t--) {
        int ans = 0;
        scanf("%d%d", &n, &k);
        for (int i = 0; i < n; i++) {
            max_ = 0;
            memset(tree, 0, sizeof(tree));
            for (int j = 0; j < k; j++) {
                scanf("%d", &e);
                Create(1, e);
            }
            for (int j = 1; j <= max_; j++) {
                if (tree[j])
                    s[i][j - 1] = '1';
                else s[i][j - 1] = '0';
            }
            s[i][max_] = '\0';
            int temp = 0;
            for (int j = 0; j < i && !temp; j++)
                if (!strcmp(s[i], s[j]))
                    temp = 1;
            if (!temp)
                ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}

法二: 

#include <bits/stdc++.h>
using namespace std;
typedef struct Node {
    Node *l, *r;
    int data, num;
}*ListNode;
int a[55][20], t;
ListNode root, p, q;
ListNode Node_T(int e, int pre_num) {
    q = (struct Node *)malloc(sizeof(Node));
    q -> data = e;
    q -> num = pre_num;
    q -> l = q -> r = NULL;
    return q;
}
void Create(ListNode &T, int e, int pre_num) {
    if (!T)
        T = Node_T(e, pre_num);
    else {
        if (e < T -> data)
            Create(T -> l, e, pre_num * 2);
        else Create(T -> r, e, pre_num * 2 + 1);
    }
}
void PrintTree(ListNode T, int i) {
    if (T) {
        a[i][t++] = T -> num;
        PrintTree(T -> l, i);
        PrintTree(T -> r, i);
    }
}
int main() {
    int T, n, k, e, flag, flsg;
    scanf("%d", &T);
    while (T--) {
        int ans = 0;
        scanf("%d%d", &n, &k);
        for (int i = 0; i < n; i++) {
            root = NULL;
            for (int j = 0; j < k; j++) {
                scanf("%d", &e);
                Create(root, e, 1);
            }
            flag = t = 0;
            PrintTree(root, i);
            for (int j = 0; j < i && !flag; j++) {
                flsg = 0;
                for (int k = 0; k < t && !flsg; k++)
                    if(a[i][k] && a[j][k] && a[i][k] != a[j][k])
                        flsg = 1;
                if (!flsg)
                    flag = 1;
            }
            if (!flag)
                ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}
相关推荐
<p> 课程演示环境:Windows10  </p> <p> 需要学习<span>Ubuntus</span>系统<span>YOLOv4-tiny</span>的同学请前往《<span>YOLOv4-tiny</span>目标检测实战:训练自己的数据集》 <span></span> </p> <p> <span> </span> </p> <p> <span style="color:#E53333;">YOLOv4-tiny</span><span style="color:#E53333;">来了!速度大幅提升!</span><span></span> </p> <p> <span> </span> </p> <p> <span>YOLOv4-tiny</span>在<span>COCO</span>上的性能可达到:<span>40.2% AP50, 371 FPS (GTX 1080 Ti)</span>。相较于<span>YOLOv3-tiny</span>,<span>AP</span>和<span>FPS</span>的性能有巨大提升。并且,<span>YOLOv4-tiny</span>的权重文件只有<span>23MB</span>,适合在移动端、嵌入式设备、边缘计算设备上部署。<span></span> </p> <p> <span> </span> </p> <p> 本课程将手把手地教大家使用<span>labelImg</span>标注和使用<span>YOLOv4-tiny</span>训练自己的数据集。课程实战分为两个项目:单目标检测(足球目标检测)和多目标检测(足球和梅西同时检测)。<span></span> </p> <p> <span> </span> </p> <p> 本课程的<span>YOLOv4-tiny</span>使用<span>AlexAB/darknet</span>,在<span>Windows10</span>系统上做项目演示。包括:<span>YOLOv4-tiny</span>的网络结构、安装<span>YOLOv4-tiny</span>、标注自己的数据集、整理自己的数据集、修改配置文件、训练自己的数据集、测试训练出的网络模型、性能统计<span>(mAP</span>计算<span>)</span>和先验框聚类分析。 <span> </span> </p> <p> <span> </span> </p> <p> 除本课程《<span>Windows</span>版<span>YOLOv4-tiny</span>目标检测实战:训练自己的数据集》外,本人推出了有关<span>YOLOv4</span>目标检测的系列课程。请持续关注该系列的其它视频课程,包括:<span></span> </p> <p> 《<span>Windows</span>版<span>YOLOv4</span>目标检测实战:训练自己的数据集》<span></span> </p> <p> 《<span>Windows</span>版<span>YOLOv4</span>目标检测实战:人脸口罩佩戴识别》<span></span> </p> <p> 《<span>Windows</span>版<span>YOLOv4</span>目标检测实战:中国交通标志识别》<span></span> </p> <p> 《<span>Windows</span>版<span>YOLOv4</span>目标检测:原理与源码解析》<span></span> </p> <p> <span> <img alt="" src="https://img-bss.csdnimg.cn/202007061503586145.jpg" /></span> </p> <p> <span><img alt="" src="https://img-bss.csdnimg.cn/202007061504169339.jpg" /><br /> </span> </p>
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页