hdu5444 Elven Postman(SBT)

就是一颗SBT的插入  插入的时候路径。最后询问的时候输出该路径即可。

比赛的时候读错题了  以为已经输出过的节点不能再输出了  浪费了很多时间。

写了个BFS -  -

为了省编码时间最后改成求路径也是BFS搞的 -  -

好菜啊TAT


#include <bits/stdc++.h>
using namespace std;
const int MAXN=2000;
struct Node
{
    Node*ch[2],*p;
    int v,has;
    void init(int _v)
    {
        v=_v;
        p=ch[0]=ch[1]=0;
        has=0;
    }
    int cmp(int x) const
    {
        if(x==v)    return -1;
        return x<v?0:1;
    }
}nodePool[MAXN],*cur,*rt,*st[MAXN];
string ans[MAXN];
struct data
{
    string s;
    Node*to;
    data(string s,Node *to):s(s),to(to){}
};
int n;
struct Tree
{
    Node* newNode(int v=0)
    {
        cur->init(v);
        return cur++;
    }
    void init()
    {
        cur=nodePool;
        rt=NULL;
    }
    void insert(int x,Node* &o=rt)
    {
        if(o==NULL)
        {
            o = newNode(x);
            st[x]=o;
        }
        else
        {
            int d=(x<o->v?0:1);
            insert(x,o->ch[d]);
            o->ch[d]->p=o;
        }
    }
    queue<data>q;
    void bfs()
    {
        while(!q.empty())
            q.pop();
        for(int i=1;i<=n;i++)
            ans[i]="";
        if(rt==NULL)
            return;
        q.push(data("",rt));
        while(!q.empty())
        {
            data c=q.front();
            q.pop();
            Node*u=c.to;
            ans[u->v]=c.s;
            if(u->ch[0])
                q.push(data(c.s+"E",u->ch[0]));
            if(u->ch[1])
                q.push(data(c.s+"W",u->ch[1]));
        }
    }
    /*
    int find(int v)
    {
        Node *o=st[v];
        int cnt=ans[v].length();
        while(o->p)
        {
            printf("%d has %d\n",o->v,o->has);
            if(o->has)
                break;
            cnt--;
            o->has=1;
            o=o->p;
        }
        return cnt;
    }
    */
}soul;
int main()
{
    int T,v,m;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        soul.init();
        for(int i=0;i<n;i++)
        {
            scanf("%d",&v);
            soul.insert(v);
        }
        soul.bfs();
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d",&v);
            printf("%s\n",ans[v].c_str());
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值