就是一颗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;
}