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=202501;
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
//*************************************************************************************int flag1=0,flag2=0,flag3=0,flag4=0;
int n;
int a[maxn];
void dfs(int x)
{if(flag1==0)return;if(a[x*2]!=0){if(a[x*2]<a[x])flag1=0;dfs(2*x);}if(a[x*2+1]!=0){if(a[x*2+1]<a[x])flag1=0;dfs(2*x+1);}
}void dfs3(int x)
{if(flag4==0)return;if(a[x*2]!=0){if(a[x*2]>a[x])flag4=0;dfs3(2*x);}if(a[x*2+1]!=0){if(a[x*2+1]>a[x])flag4=0;dfs3(2*x+1);}
}
void dfs1(int x)
{if(flag2==0)return;if(a[x*2]!=0){if(a[x*2]<=a[x])flag2=0;dfs1(2*x);}if(a[x*2+1]!=0){if(a[x*2+1]>=a[x])flag2=0;dfs1(2*x+1);}
}
void dfs2(int x)
{if(flag3==0)return;if(a[x*2]!=0){if(a[x*2]>=a[x])flag3=0;dfs2(2*x);}if(a[x*2+1]!=0){if(a[x*2+1]<=a[x])flag3=0;dfs2(2*x+1);}
}
int main()
{int t=read();for(int cas=1;cas<=t;cas++){memset(a,0,sizeof(a));flag1=1,flag2=1,flag3=1,flag4=1;n=read();for(int i=1;i<=n;i++)a[i]=read();dfs(1);flag2=1;dfs1(1);flag3=1;dfs2(1);flag4=1;dfs3(1);//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);}
}

转载于:https://www.cnblogs.com/qscqesze/p/4678362.html

CDOJ 483 Data Structure Problem DFS相关推荐

  1. HDU - 7072 Boring data structure problem 双端队列 + 思维

    传送门 文章目录 题意: 思路: 题意: 你需要实现如下四个操作 q≤1e7q\le1e7q≤1e7 思路: 做的时候想了个链表的思路让队友写了,懒. 看了题解感觉题解还是很妙的. 你需要快速插入一个 ...

  2. 2020ICPC(小米邀请赛2) - Data Structure Problem(线段树+树状数组)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数列 a 和数列 b,然后需要维护一个前缀和 c,c 的定义如下:c[ i ] = max( c[ i - 1 ] + b[ i ] , a[ i ...

  3. Boring data structure problem 模拟-双端队列

    题意 : 维护一个"双端队列",1e7次操作,支持左插入,右插入,按值删除,查找中点(靠右)值ceil[(m+1)/2]ceil[(m + 1) / 2]ceil[(m+1)/2] ...

  4. Data Structure Problem

    试题链接 题目描述 题意: 有两个序列, 操作1是将a序列的第x位改成y 操作2是将b序列的第x位改成y 操作3是找到一个cx,满足递推式c0=0,ci = max(ci-1+bi,ai) 题解: 官 ...

  5. leetcode 211. Add and Search Word - Data structure design Trie树

    题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...

  6. HDU - 6967 G I love data structure 线段树维护矩阵 + 细节

    传送门 文章目录 题意: 思路: 题意: 给你两个长度为nnn的数组a,ba,ba,b,你需要完成如下四种操作: 思路: 思路还是比较简单的,首先建一颗线段树,线段树中维护a,b,a2,b2,aba, ...

  7. 【HDU - 4217 】Data Structure? (线段树求第k小数)

    题干: Data structure is one of the basic skills for Computer Science students, which is a particular w ...

  8. Data Structure

    Data Structure 1. Abstract Data Type (ADT) 1.1. Data type A set of objects + a set of operations Exa ...

  9. CF data structure 自制题单(一)

    CF data structure 2000~2100 为你的战斗,献上雷鸣般的喝彩!--唔姆 目标 30 道题 1. Problem - 1555E - Codeforces 看了提示 给一些线段, ...

  10. LeetCode Two Sum III - Data structure design

    原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...

最新文章

  1. linux yield_linux内核的一些知识点(上)
  2. Angular从零到一1.2 环境配置要求
  3. YOU SAY quot;THE RULE IS HEREquot;,BUT I SAY quot;WHY?quot;
  4. python中的帮助_在Python中使用help帮助
  5. android多行文本框hint居中,在安卓等移动浏览器中placeholder中的文字不垂直居中问题...
  6. C++ :Signal: SIGSEGV (Segmentation fault) ,深拷贝
  7. Java学习正嗨Day2!
  8. 如何验证python的下载安装_如何下载python并正确安装
  9. 用python重构策略模式
  10. AddLinkedServer
  11. 计算机科学与python编程导论_【基于Python】MIT OCW 计算机科学与编程导论
  12. loss函数之L1Loss,MSELoss,SmoothL1Loss, HuberLoss
  13. JPress v2.0-rc.6 发布,新增 Java 插件的在线升级功能
  14. 如何配置FMS边缘服务器
  15. 如何通过网页超链接控制电脑应用程序
  16. Pytorch聊天机器人
  17. MiniGUI移植过程
  18. 最新emoji表情代码大全_中老年表情包:微信朋友圈早晨问候语带图片 最新早上好问候语动态图片大全...
  19. 计算机内无法使用搜狗,技巧:IE11无法使用搜狗输入法的原因及解决方法
  20. 苹果商标计算机辅助设计,苹果 Apple MacBook Pro 15英寸 2019 详细评测报告

热门文章

  1. 微信小程序——尤克里里和弦查询
  2. Redis 禁止使用耗时命令和时间复杂度为O(n)的命令
  3. calender获取日期前几月_iOS时间,日期,星期等相关获取
  4. 素数就是不能再进行等分的数。比如2,3,5,7,11,等 9=3*3说明它可以等分,因而不是素数 我们国家在1949年建国,如果只给你 1 9 4 9 这4个数字卡片, 可以随意摆放他们的先后顺序(但
  5. java 避免重定向_java – 避免循环重定向使用HttpClient 4.1.1
  6. 内核功能导致重启_错误信息:VS2010 Profiler导致Win7重启
  7. python 字典处理_Python3 字典 in 操作符
  8. (day 52 - 二叉搜索树) 剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
  9. python训练营 朋友圈点赞收费吗_微信朋友圈点赞,是不是扣话费?有人给
  10. python牛顿法与拟牛顿法_python实现牛顿法求解求解最小值(包括拟牛顿法)【最优化课程笔记】...