码迷,mamicode.com
首页 > 其他好文 > 详细

CDOJ 483 Data Structure Problem DFS

时间:2015-08-21 01:46:57      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

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 (1N1000), 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 }

 

CDOJ 483 Data Structure Problem DFS

标签:

原文地址:http://www.cnblogs.com/diang/p/4746674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!