Data Structure Problem

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.uestc.edu.cn/#/problem/show/483

Description

Data structure is a fundamental course of Computer Science, so that each contestant is highly likely to solve this data structure problem.

A Heap data structure is a binary tree with the following properties:

It is a complete binary tree; that is, each level of the tree is completely filled, except possibly the bottom level. At this level, it is filled from left to right.
It satisfies the heap-order property: The key stored in each node is greater than or equal to the keys stored in its children.
So such a heap is sometimes called a max-heap. (Alternatively, if the comparison is reversed, the smallest element is always in the root node, which results in a min-heap.)

A binary search tree (BST), which may sometimes also be called an ordered or sorted binary tree, is a node-based binary tree data structure which has the following properties:

The left subtree of a node contains only nodes with keys less than (greater than) the node's key.
The right subtree of a node contains only nodes with keys greater than (less than) the node's key.
Both the left and right subtrees must also be binary search trees.
Given a complete binary tree with $N$ keys, your task is to determine the type of it.

Note that either a max-heap or a min-heap is acceptable, and it is also acceptable for both increasing ordered BST and decreasing ordered BST.

Input

The first line of the input is $T$ (no more than $100$), which stands for the number of test cases you need to solve.

For each test case, the first line contains an integer $N$ ($1 \leq N \leq 1000$), indicating the number of keys in the binary tree. On the second line, a permutation of $1$ to $N$ is given. The key stored in root node is given by the first integer, and the $2i_{th}$ and $2i+1_{th}$ integers are keys in the left child and right child of the $i_{th}$ integer respectively.

Output

For every test case, you should output Case #k: first, where $k$ indicates the case number and counts from $1$. Then output the type of the binary tree:

Neither — It is neither a Heap nor a BST.
Both — It is both a Heap and a BST.
Heap — It is only a Heap.
BST — It is only a BST.

Sample Input

4
1
1
3
1 2 3
3
2 1 3
4
2 1 3 4

Sample Output

Case #1: Both
Case #2: Heap
Case #3: BST
Case #4: Neither

HINT

题意

给你n个数,然后这n个数构成的二叉树,是平衡二叉树还是堆

题解:

直接dfs就好了

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
const int maxn=;
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************* int flag1=,flag2=,flag3=,flag4=;
int n;
int a[maxn];
void dfs(int x)
{
if(flag1==)
return;
if(a[x*]!=)
{
if(a[x*]<a[x])
flag1=;
dfs(*x);
}
if(a[x*+]!=)
{
if(a[x*+]<a[x])
flag1=;
dfs(*x+);
}
} void dfs3(int x)
{
if(flag4==)
return;
if(a[x*]!=)
{
if(a[x*]>a[x])
flag4=;
dfs3(*x);
}
if(a[x*+]!=)
{
if(a[x*+]>a[x])
flag4=;
dfs3(*x+);
}
}
void dfs1(int x)
{
if(flag2==)
return;
if(a[x*]!=)
{
if(a[x*]<=a[x])
flag2=;
dfs1(*x);
}
if(a[x*+]!=)
{
if(a[x*+]>=a[x])
flag2=;
dfs1(*x+);
}
}
void dfs2(int x)
{
if(flag3==)
return;
if(a[x*]!=)
{
if(a[x*]>=a[x])
flag3=;
dfs2(*x);
}
if(a[x*+]!=)
{
if(a[x*+]<=a[x])
flag3=;
dfs2(*x+);
}
}
int main()
{
int t=read();
for(int cas=;cas<=t;cas++)
{
memset(a,,sizeof(a));
flag1=,flag2=,flag3=,flag4=;
n=read();
for(int i=;i<=n;i++)
a[i]=read();
dfs();
flag2=;
dfs1();
flag3=;
dfs2();
flag4=;
dfs3();
//cout<<flag1<<" "<<flag2<<" "<<flag3<<" "<<flag4<<endl;
if((flag1||flag4)&&(flag2||flag3))
printf("Case #%d: Both\n",cas);
else if((flag1||flag4)&&!(flag2||flag3))
printf("Case #%d: Heap\n",cas);
else if(!(flag1||flag4)&&(flag2||flag3))
printf("Case #%d: BST\n",cas);
else if(!(flag1||flag4)&&!(flag2||flag3))
printf("Case #%d: Neither\n",cas);
}
}

CDOJ 483 Data Structure Problem DFS的更多相关文章

  1. ZOJ 4009 And Another Data Structure Problem(ZOJ Monthly, March 2018 Problem F,发现循环节 + 线段树 + 永久标记)

    题目链接  ZOJ Monthly, March 2018 Problem F 题意很明确 这个模数很奇妙,在$[0, mod)$的所有数满足任意一个数立方$48$次对$mod$取模之后会回到本身. ...

  2. [hdu7099]Just Another Data Structure Problem

    不难发现,问题即求满足以下条件的$(i,j)$对数: 1.$1\le i<j\le n$且$a_{i}=a_{j}$ 2.$\min_{i\le k\le j}y_{k}\ge l$且$\max ...

  3. [hdu7097]Just a Data Structure Problem

    (四边形不等式的套路题) 对于某一组$a_{i}$,显然可以区间dp,设$f_{l,r}$表示区间$[l,r]$​的答案,则转移即$$f_{l,r}=\begin{cases}0&(l=r)\ ...

  4. HDU 6649 Data Structure Problem(凸包+平衡树)

    首先可以证明,点积最值的点对都是都是在凸包上,套用题解的证明:假设里两个点都不在凸包上, 考虑把一个点换成凸包上的点(不动的那个点), 不管你是要点积最大还是最小, 你都可以把那个不动的点跟原点拉一条 ...

  5. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  6. 211. Add and Search Word - Data structure design

    题目: Design a data structure that supports the following two operations: void addWord(word) bool sear ...

  7. Add and Search Word - Data structure design 解答

    Question Design a data structure that supports the following two operations: void addWord(word) bool ...

  8. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...

  9. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

随机推荐

  1. MySQL压测中遇到的一些问题

    批量insert http://blog.csdn.net/xiaoxian8023/article/details/20155429 Mysql jdbc 批处理数据,需要给jdbc连接加上rewr ...

  2. 常见设计模式的解析和实现(C++)之九—Decorator模式

    作用:动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator模式相比生成子类更为灵活. UML结构图: 抽象基类: 1)  Component :定义一个对象接口,可以为这个接口动态地 ...

  3. 将数据库从普通文件系统迁移到ASM中

    数据库存储的是普通的文件系统,现在将数据库迁移到ASM存储中. 准备ASM环境: [oracle@kel ~]$ asmcmd ASMCMD> ls ASM/ KEL/ ASMCMD> 在 ...

  4. Core Java 学习笔记——2.基本数据类型&类型转换

    数据类型(8种基本类型:int/short/long/byte/float/double/char/boolean) 整型 int 4字节 -2 147 483 648~2 147 483 647 s ...

  5. .net mvc HtmlHelper扩展使用

    如果是你是从webform开始接触.net,你应该记得webform开发中,存在自定义控件这东西,它使得我们开发起来十分方便,如今mvc大势所趋,其实在mvc开发时,也存在自定义控件这么个东西,那就是 ...

  6. vs2010 无法连接到asp.net development server

    http://blog.csdn.net/xqf309/article/details/7881257 今天打开之前的程序,按了F5进行调试,等了会弹出窗体来说:无法连接到asp.net develo ...

  7. 内核源码分析之软中断(基于3.16-rc4)

    1.和软中断相关的数据结构: softing_vec数组(kernel/softirq.c) static struct softirq_action softirq_vec[NR_SOFTIRQS] ...

  8. 搭建linux下teamspeak3多人语音服务器

    最近项目中新的需求,需要支持多人在线实时通话.就安装测试一下teamspeak.http://www.teamspeak.com/ 主页有服务器版本和客户端版本供下载安装.软硬件环境: melot@m ...

  9. 第二百八十八天 how can I坚持

    明天,就要回济南了.早上八点的票,去火车站还得一个半小时,六点就得起床啊,好早,忍了. 这两天又没法更新日志了.周一才能回来. 今天好忙,事情好杂. 其实不想请假. 算了,睡觉了,没什么可写的了.是没 ...

  10. curl命令的基本用法

    我们知道在linux环境下,可以调用curl下载网页. 但curl有些高级的应用,只需要几行命令行,可能比你写多行php.python.C++的程序要快些. 下面从问题驱动的角度来谈谈curl的用法 ...