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就好了

代码

  1. #include <cstdio>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <ctime>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <set>
  8. #include <vector>
  9. #include <sstream>
  10. #include <queue>
  11. #include <typeinfo>
  12. #include <fstream>
  13. #include <map>
  14. #include <stack>
  15. typedef long long ll;
  16. using namespace std;
  17. //freopen("D.in","r",stdin);
  18. //freopen("D.out","w",stdout);
  19. #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
  20. #define test freopen("test.txt","r",stdin)
  21. const int maxn=;
  22. #define mod 1000000007
  23. #define eps 1e-9
  24. const int inf=0x3f3f3f3f;
  25. const ll infll = 0x3f3f3f3f3f3f3f3fLL;
  26. inline ll read()
  27. {
  28. ll x=,f=;char ch=getchar();
  29. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  30. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  31. return x*f;
  32. }
  33. //*************************************************************************************
  34.  
  35. int flag1=,flag2=,flag3=,flag4=;
  36. int n;
  37. int a[maxn];
  38. void dfs(int x)
  39. {
  40. if(flag1==)
  41. return;
  42. if(a[x*]!=)
  43. {
  44. if(a[x*]<a[x])
  45. flag1=;
  46. dfs(*x);
  47. }
  48. if(a[x*+]!=)
  49. {
  50. if(a[x*+]<a[x])
  51. flag1=;
  52. dfs(*x+);
  53. }
  54. }
  55.  
  56. void dfs3(int x)
  57. {
  58. if(flag4==)
  59. return;
  60. if(a[x*]!=)
  61. {
  62. if(a[x*]>a[x])
  63. flag4=;
  64. dfs3(*x);
  65. }
  66. if(a[x*+]!=)
  67. {
  68. if(a[x*+]>a[x])
  69. flag4=;
  70. dfs3(*x+);
  71. }
  72. }
  73. void dfs1(int x)
  74. {
  75. if(flag2==)
  76. return;
  77. if(a[x*]!=)
  78. {
  79. if(a[x*]<=a[x])
  80. flag2=;
  81. dfs1(*x);
  82. }
  83. if(a[x*+]!=)
  84. {
  85. if(a[x*+]>=a[x])
  86. flag2=;
  87. dfs1(*x+);
  88. }
  89. }
  90. void dfs2(int x)
  91. {
  92. if(flag3==)
  93. return;
  94. if(a[x*]!=)
  95. {
  96. if(a[x*]>=a[x])
  97. flag3=;
  98. dfs2(*x);
  99. }
  100. if(a[x*+]!=)
  101. {
  102. if(a[x*+]<=a[x])
  103. flag3=;
  104. dfs2(*x+);
  105. }
  106. }
  107. int main()
  108. {
  109. int t=read();
  110. for(int cas=;cas<=t;cas++)
  111. {
  112. memset(a,,sizeof(a));
  113. flag1=,flag2=,flag3=,flag4=;
  114. n=read();
  115. for(int i=;i<=n;i++)
  116. a[i]=read();
  117. dfs();
  118. flag2=;
  119. dfs1();
  120. flag3=;
  121. dfs2();
  122. flag4=;
  123. dfs3();
  124. //cout<<flag1<<" "<<flag2<<" "<<flag3<<" "<<flag4<<endl;
  125. if((flag1||flag4)&&(flag2||flag3))
  126. printf("Case #%d: Both\n",cas);
  127. else if((flag1||flag4)&&!(flag2||flag3))
  128. printf("Case #%d: Heap\n",cas);
  129. else if(!(flag1||flag4)&&(flag2||flag3))
  130. printf("Case #%d: BST\n",cas);
  131. else if(!(flag1||flag4)&&!(flag2||flag3))
  132. printf("Case #%d: Neither\n",cas);
  133. }
  134. }

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. 【转】cocos2d-x Lua

    Call custom c++ from Lua cocos2d-x lua binds c++ class, class functions ,enum and some global functi ...

  2. Google的通用翻译机能成为未来的巴别鱼吗?

    “巴别鱼,”<银河系漫游指南>轻轻朗读着,“体型很小,黄色,外形像水蛭,很可能是宇宙中最奇异的事物.它靠接收脑电波的能量为生,并且不是从其携带者身上接收,而是从周围的人身上.……如果你把一 ...

  3. longest common str

    #include <vector> #include <iostream> #include <string> using namespace std; int l ...

  4. codeforces 678E Another Sith Tournament 概率dp

    奉上官方题解 然后直接写的记忆化搜索 #include <cstdio> #include <iostream> #include <ctime> #include ...

  5. Android应用启动时间及启动日志获取方法

    1. Android应用中,可以使用如下方式进行应用启动时间的查看 2. 启动日志获取方法:

  6. LeetCode题解——String to Integer(atoi)

    题目: 字符串转换为数字. 解法: 这道题的意思是要考虑到,如果有前置的空字符,则跳过:如果超出数字范围,则返回最大/最小整数:如果碰到第一个不能转换的字符,则返回. 代码: class Soluti ...

  7. Codevs No.1163 访问艺术馆

    2016-05-31 20:48:47 题目链接: 访问艺术馆 (Codevs No.1163) 题目大意: 一个贼要在一个二叉树结构的艺术馆中偷画,画都处于叶子节点处,偷画和经过走廊都需要时间,求在 ...

  8. SRM DIV1 500pt DP

    SRM 501 DIV1 500pt SRM 502 DIV1 500pt SRM 508 DIV1 500pt SRM 509 DIV1 500pt SRM 511 DIV1 500pt SRM 5 ...

  9. Android实例-读取设备联系人(XE8+小米2)

    相关资料: http://www.colabug.com/thread-1071065-1-1.html 结果: 1.将权限打开Read contacts设置为True,不然报图一的错误. 2.搜索空 ...

  10. UVALive 7278 Game of Cards (sg函数)

    Game of Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/G Description Alice and Bob ...