PAT L3-016:二叉搜索树的结构(暴力)

https://www.patest.cn/contests/gplt/L3-016

题意:中文。

思路:暴力构造,暴力查询就好了。只不过操作很多,很麻烦。比赛的时候再给我10分钟就打完了,手速太慢好可惜。

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 #define N 100010
  4 typedef long long LL;
  5 struct Node {
  6     int num;
  7     Node *l, *r, *fa;
  8     Node (int n) {
  9         l = r = fa = NULL;
 10         num = n;
 11     }
 12 } ;
 13 Node *root;
 14 int flag, d;
 15 char s[5][15];
 16 
 17 void Insert(Node *now, Node *f, int x, int kind) {
 18     if(now == NULL) {
 19         now = new Node(x);
 20         now->fa = f;
 21         if(kind) f->r = now;
 22         else f->l = now;
 23         return ;
 24     }
 25     if(x > now->num) Insert(now->r, now, x, 1);
 26     else Insert(now->l, now, x, 0);
 27 }
 28 
 29 Node *Find(int x) {
 30     Node *now = root; d = 0;
 31     while(now != NULL && now->num != x) {
 32         if(x > now->num) now = now->r;
 33         else now = now->l;
 34         d++;
 35     }
 36     return now;
 37 }
 38 
 39 void Solve1(int x) {
 40     if(root && root->num == x) flag = 1;
 41 }
 42 
 43 void Solve2(int x, int y) {
 44     Node *now1 = Find(x), *now2 = Find(y);
 45     if(now1 && now2 && now1->fa == now2->fa) flag = 1;
 46 }
 47 
 48 void Solve3(int x, int y) {
 49     Node *now = Find(y);
 50     if(now && now->fa && now->fa->num == x) flag = 1;
 51 }
 52 
 53 void Solve4(int x, int y) {
 54     Node *now = Find(y);
 55     if(now && now->l && now->l->num == x) flag = 1;
 56 }
 57 
 58 void Solve5(int x, int y) {
 59     Node *now = Find(y);
 60     if(now && now->r && now->r->num == x) flag = 1;
 61 }
 62 
 63 void Solve6(int x, int y) {
 64     Node *now1 = Find(x);
 65     int d1 = d;
 66     Node *now2 = Find(y);
 67     int d2 = d;
 68     if(now1 && now2 && d1 == d2) flag = 1;
 69 }
 70 
 71 int main() {
 72     int n, a, b; scanf("%d", &n);
 73     if(n) scanf("%d", &a), root = new Node(a);
 74     for(int i = 1; i < n; i++) scanf("%d", &a), Insert(root, NULL, a, 0);
 75     int q; scanf("%d", &q);
 76     while(q--) {
 77         flag = 0;
 78         scanf("%d", &a);
 79         scanf("%s", s[0]);
 80         if(s[0][0] == 'a') {
 81             scanf("%d", &b);
 82             scanf("%s %s", s[1], s[2]);
 83             if(s[2][0] == 's') {
 84                 Solve2(a, b);
 85             } else {
 86                 Solve6(a, b);
 87                 scanf("%s %s %s", s[3], s[3], s[3]);
 88             }
 89         } else {
 90             scanf("%s %s", s[1], s[2]);
 91             if(s[2][1] == 'o') {
 92                 Solve1(a);
 93             } else if(s[2][1] == 'a') {
 94                 scanf("%s %d", s[3], &b), Solve3(a, b);
 95             } else if(s[2][1] == 'e') {
 96                 scanf("%s %s %d", s[3], s[4], &b), Solve4(a, b);
 97             } else {
 98                 scanf("%s %s %d", s[3], s[4], &b), Solve5(a, b);
 99             }
100         }
101         if(flag) puts("Yes");
102         else puts("No");
103     }
104     return 0;
105 }

 

转载于:https://www.cnblogs.com/fightfordream/p/6727488.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值