团体程序设计天梯赛(L3-016 二叉搜索树的结构 (30 分))

题目:

思路分析:

拿数组建树 但是n最大100 只能拿27分

建议别建立标准的二叉树 可以借助lson rson 的数组来存

这条思路有时间写! 

代码实现:

/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 */


const int MAX=1001;
int n;
int a[MAX];
map<int,int>mp;
int js(int x){
    int ans=0;
    while (x/=2) {
        ans++;
    }
    return ans;
}
void built(int pos,int x){
    if(a[pos]==-1){
        a[pos]=x;
        mp[x]=pos;
        return;
    }
    if(a[pos]<x){
        built(pos<<1|1, x);
    }
    else built(pos<<1, x);
}
int main(){
    cin>>n;
    mms(a,-1);
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        built(1,x);
    }

//    for(int i=1;i<=7;i++){
//        cout<<a[i]<<" ";
//    }
//    A is the root,即"A是树的根";
//    A and B are siblings,即"A和B是兄弟结点";
//    A is the parent of B,即"A是B的双亲结点";
//    A is the left child of B,即"A是B的左孩子";
//    A is the right child of B,即"A是B的右孩子";
//    A and B are on the same level,即"A和B在同一层上"。
    int k;
    cin>>k;
    while (k--) {
        int x;
        int y;
        string s;
        cin>>x>>s;
        if(s=="is"){
            cin>>s;
            cin>>s;
            if(s=="root"){
//                cout<<"root"<<endl;
                if(a[1]==x){
                    cout<<"Yes"<<endl;
                }
                else cout<<"No"<<endl;
                
            }
            else if(s=="parent"){
                
//                cout<<"parent"<<endl;
                
                cin>>s;
                cin>>y;
//                cout<<x<<" "<<y<<endl;
                if(mp[y]/2==mp[x]){
                    cout<<"Yes"<<endl;
                }
                else cout<<"No"<<endl;
            }
            else if(s=="left"){
//                cout<<"left"<<endl;
//                cout<<x<<" "<<y<<endl;
                cin>>s;
                cin>>s;
                cin>>y;
                if(mp[y]*2==mp[x]){
                    cout<<"Yes"<<endl;
                }
                else cout<<"No"<<endl;
            }
            else if(s=="right"){
//                cout<<"right"<<endl;
                cin>>s;
                cin>>s;
                cin>>y;
//                cout<<x<<" "<<y<<endl;
//                cout<<mp[y]<<" "<<endl;
//                cout<<mp[x]<<" "<<endl;
                if(mp[y]*2+1==mp[x]){
                    cout<<"Yes"<<endl;
                }
                else cout<<"No"<<endl;
            }
            
        }
        //    A is the root,即"A是树的根";
        //    A and B are siblings,即"A和B是兄弟结点";
        //    A is the parent of B,即"A是B的双亲结点";
        //    A is the left child of B,即"A是B的左孩子";
        //    A is the right child of B,即"A是B的右孩子";
        //    A and B are on the same level,即"A和B在同一层上"。
        else if(s=="and"){
            cin>>y;
            cin>>s;
            cin>>s;
            if(s=="on"){
//                cout<<"same"<<endl;
//                cout<<x<<" "<<y<<endl;
                cin>>s;
                cin>>s;
                cin>>s;
                x=js(mp[x]);
                y=js(mp[y]);
//                cout<<x<<" "<<y<<endl;
                if(x==y){
                    cout<<"Yes"<<endl;
                }
                else cout<<"No"<<endl;
            }
            else {
//                cout<<"bro"<<endl;
//                cout<<x<<" "<<y<<endl;
                if(mp[x]/2==mp[y]/2){
                    cout<<"Yes"<<endl;
                }
                else cout<<"No"<<endl;
            }
            
        }
//        cout<<k<<endl;
//        cout<<endl<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/m0_57006708/article/details/121266216