将一系列给定数字顺序插入一个初始为空的小顶堆H[]。随后判断一系列相关命题是否为真。命题分下列几种:
“x is the root”:x是根结点;“x and y are siblings”:x和y是兄弟结点;“x is the parent of y”:x是y的父结点;“x is a child of y”:x是y的一个子结点。输入格式:
每组测试第1行包含2个正整数N(<= 1000)和M(<= 20),分别是插入元素的个数、以及需要判断的命题数。下一行给出区间[-10000, 10000]内的N个要被插入一个初始为空的小顶堆的整数。之后M行,每行给出一个命题。题目保证命题中的结点键值都是存在的。
输出格式:
对输入的每个命题,如果其为真,则在一行中输出“T”,否则输出“F”。
输入样例:5 446 23 26 24 1024 is the root26 and 23 are siblings46 is the parent of 2323 is a child of 10输出样例:FTFT#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;int a[1009],n,m;char ch[1009];struct node{ int id,val; friend bool Operator <(node x,node y) { return x.val<y.val; }}b[1009];int get(int k){ int l=1,r=n,ans; while(l<=r) { int mid=(l+r)>>1; if(b[mid].val==k) { ans=b[mid].id; break; } if(b[mid].val<k) l=mid+1; else r=mid-1; } return ans;}int main(){ scanf("%d %d",&n,&m); for(int i=1; i<=n; i++) { scanf("%d",&a[i]); int k=i; while(k>1&&a[k]<a[k/2]) { swap(a[k],a[k/2]); k/=2; } } for(int i=1; i<=n; i++) b[i].id=i,b[i].val=a[i]; sort(b+1,b+1+n); while(m--) { int x,y; scanf("%d%s",&x,ch); if(!strcmp(ch,"and")) { scanf("%d%s%s",&y,ch,ch); if(get(x)/2==get(y)/2) PRintf("T/n"); else printf("F/n"); continue; } scanf("%s",ch); if(!strcmp(ch,"a")) { scanf("%s%s%d",ch,ch,&y); if(get(x)/2==get(y)) printf("T/n"); else printf("F/n"); continue; } scanf("%s",ch); if(!strcmp(ch,"root")) { if(get(x)==1) printf("T/n"); else printf("F/n"); continue; } scanf("%s%d",ch,&y); if(get(y)/2==get(x)) printf("T/n"); else printf("F/n"); } return 0;}
新闻热点
疑难解答