[Violence] vijos p1897 xuejie eats steak

Source: Internet
Author: User

Judge heap: recursively determine whether each node's child is larger (smaller) than its father ).

Determine whether BST is ordered by the middle-order traversal.

 1 #include<cstdio> 2 using namespace std; 3 #define lc (rt<<1) 4 #define rc (rt<<1|1) 5 int T,n,a[1001],b[1001],en; 6 void Mid(int rt) 7 { 8     if(lc<=n) Mid(lc); 9     b[++en]=a[rt];10     if(rc<=n) Mid(rc);11 }12 bool is_BST()13 {14     bool f1=1,f2=1; en=0; Mid(1);15     for(int i=2;i<=n;i++) if(b[i]<b[i-1]) {f1=0; break;}16     if(f1) return 1;17     for(int i=2;i<=n;i++) if(b[i]>b[i-1]) {f2=0; break;}18     if(f2) return 1;19     return 0;20 }21 bool is_Heap(int rt)22 {23     if(lc<=n)24       {25           if(a[lc]>a[rt]) return 0;26           if(!is_Heap(lc)) return 0;27       }28     if(rc<=n)29       {30           if(a[rc]>a[rt]) return 0;31           if(!is_Heap(rc)) return 0;32       }33     return 1;34 }35 bool is_Heap_2(int rt)36 {37     if(lc<=n)38       {39           if(a[lc]<a[rt]) return 0;40           if(!is_Heap_2(lc)) return 0;41       }42     if(rc<=n)43       {44           if(a[rc]<a[rt]) return 0;45           if(!is_Heap_2(rc)) return 0;46       }47     return 1;48 }49 int main()50 {51     scanf("%d",&T);52     for(int i=1;i<=T;i++)53       {54           printf("Case #%d: ",i);55           scanf("%d",&n);56           for(int j=1;j<=n;j++) scanf("%d",&a[j]);57           bool flag1=0,flag2=0;58           if(is_BST()) flag1=1;59           if(is_Heap(1)) flag2=1;60           if((!flag2)) if(is_Heap_2(1)) flag2=1;61           if(flag1&&flag2) puts("Both");62           else if(flag1) puts("BST");63           else if(flag2) puts("Heap");64           else puts("Neither");65       }66     return 0;67 }

 

[Violence] vijos p1897 xuejie eats steak

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.