CDOJ 483 Data Structure Problem DFS

32‘20

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≤N≤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 2ith and 2i+1thintegers are keys in the left child and right child of the ith 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





1 2 3 

2 1 3 

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 #define test freopen("1.txt","r",stdin)
 18 #define maxn 2000001
 19 #define mod 10007
 20 #define eps 1e-9
 21 const int inf=0x3f3f3f3f;
 22 const ll infll = 0x3f3f3f3f3f3f3f3fLL;
 23 inline int read()
 24 {
 25     ll x=0,f=1;
 26     char ch=getchar();
 27     while(ch<‘0‘||ch>‘9‘)
 28     {
 29         if(ch==‘-‘)f=-1;
 30         ch=getchar();
 31     }
 32     while(ch>=‘0‘&&ch<=‘9‘)
 33     {
 34         x=x*10+ch-‘0‘;
 35         ch=getchar();
 36     }
 37     return x*f;
 38 }
 39 inline void out(int x)
 40 {
 41     if(x>9) out(x/10);
 42     putchar(x%10+‘0‘);
 43 }
 44 //**************************************************************************************
 45 int a[2000];
 46 int n;
 47 bool is_Heap1(int root)
 48 {
 49     int l=root*2,r=root*2+1;
 50     int flag1=1;
 51     if(l<=n&&(a[l]<a[root]||!is_Heap1(l)))
 52         flag1=0;
 53     int flag2=1;
 54     if(r<=n&&(a[r]<a[root]||!is_Heap1(r)))
 55         flag2=0;
 56     return flag1&&flag2;
 57 }
 58 bool is_Heap2(int root)
 59 {
 60     int l=root*2,r=root*2+1;
 61     int flag1=1;
 62     if(l<=n&&(a[l]>a[root]||!is_Heap2(l)))
 63         flag1=0;
 64     int flag2=1;
 65     if(r<=n&&(a[r]>a[root]||!is_Heap2(r)))
 66         flag2=0;
 67     return flag1&&flag2;
 68 }
 69 bool is_BST1(int root)
 70 {
 71     int l=root*2,r=root*2+1;
 72     int flag1=1;
 73     if(l<=n&&(a[l]>=a[root]||!is_BST1(l)))
 74         flag1=0;
 75     int flag2=1;
 76     if(r<=n&&(a[r]<=a[root]||!is_BST1(r)))
 77         flag2=0;
 78     return flag1&&flag2;
 79 }
 80 bool is_BST2(int root)
 81 {
 82     int l=root*2,r=root*2+1;
 83     int flag1=1;
 84     if(l<=n&&(a[l]<=a[root]||!is_BST2(l)))
 85         flag1=0;
 86     int flag2=1;
 87     if(r<=n&&(a[r]>=a[root]||!is_BST2(r)))
 88         flag2=0;
 89     return flag1&&flag2;
 90 }
 91 int main()
 92 {
 93     int t;
 94     scanf("%d",&t);
 95     for(int c=1;c<=t;c++)
 96     {
 97         scanf("%d",&n);
 98         for(int i=1; i<=n; i++)
 99             scanf("%d",&a[i]);
100         int flag1,flag2;
101         flag1=is_Heap1(1)||is_Heap2(1);
102         flag2=is_BST1(1)||is_BST2(1);
103         printf("Case #%d: ",c);
104         if(flag1&&flag2)
105             printf("Both\n");
106         else if((flag1||flag2)==0)
107             printf("Neither\n");
108         else if(flag1==1)
109             printf("Heap\n");
110         else printf("BST\n");
111     }
112     return 0;
113 }
时间: 2024-10-10 23:07:10

CDOJ 483 Data Structure Problem DFS的相关文章

ZOJ 4009 And Another Data Structure Problem(ZOJ Monthly, March 2018 Problem F,发现循环节 + 线段树)

题目链接  ZOJ Monthly, March 2018 Problem F 题意很明确 这个模数很奇妙,在$[0, mod)$的所有数满足任意一个数立方$48$次对$mod$取模之后会回到本身. 所以开$48$棵线段树,和一个永久标记.当对某个区间操作时对这个区间加一层永久标记. 即当前我要查找的第$x$层,实际找的是第$up[i] + x$层. 时间复杂度$O(48nlogn)$ #include <bits/stdc++.h> using namespace std; #define

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

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

LeetCode OJ 之 Add and Search Word - Data structure design (Trie数据结构设计)

题目: Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one le

LeetCode OJ:Add and Search Word - Data structure design(增加以及搜索单词)

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

uva 11995 - I Can Guess the Data Structure!

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=3146&mosmsg=Submission+received+with+ID+14262472 I Can Guess the Data Structure! There is a bag-like data structure, supporti

hdu-5929 Basic Data Structure(双端队列+模拟)

题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 207    Accepted Submission(s): 41 Problem Description Mr. Frog learned a basic data structure recently, which is called

Leetcode: Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

Add and Search Word - Data structure design

https://leetcode.com/problems/add-and-search-word-data-structure-design/ Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string co

HDU 4217 Data Structure?(线段树 or 树状数组啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4217 Problem Description Data structure is one of the basic skills for Computer Science students, which is a particular way of storing and organizing data in a computer so that it can be used efficiently