CDOJ 483 Data Structure Problem DFS

时间:2024-08-26 17:35:14

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);
}
}