当前位置: 代码迷 >> 综合 >> DFS hdu5444 Elven Postman
  详细解决方案

DFS hdu5444 Elven Postman

热度:21   发布时间:2023-12-14 03:48:35.0

传送门:点击打开链接

题意:类似平衡二叉树,告诉你n个节点,以及n个节点的权值,要求右子节点的权值比根节点权值小,左子节点权值比根节点权值大

思路:按照大小建树,顺带保留上一个节点,然后查询的时候只要直接打印路径就行了

#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck printf("fuck")
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;const int MX = 1e4 + 5;char ans[MX];
int S[MX], TO[MX], cnt;
int L[MX], R[MX], A[MX], pre[MX], rear;void DFS(int w, int rt, int last) {if(A[rt] == -1) {pre[rt] = last;A[rt] = w;TO[w] = rt;return;}if(w > A[rt]) {if(L[rt] == -1) L[rt] = ++rear;DFS(w, L[rt], rt);} else {if(R[rt] == -1) R[rt] = ++rear;DFS(w, R[rt], rt);}
}void query(int rt) {if(pre[rt] != -1) {query(pre[rt]);int v = pre[rt];if(A[rt] > A[v]) ans[cnt++] = 'W';else ans[cnt++] = 'E';}
}int main() {int T, n, Q; //FIN;scanf("%d", &T);while(T--) {rear = 0;memset(A, -1, sizeof(A));memset(L, -1, sizeof(L));memset(R, -1, sizeof(R));scanf("%d", &n);for(int i = 1; i <= n; i++) {scanf("%d", &S[i]);}int rt = ++rear;for(int i = 1; i <= n; i++) {DFS(S[i], rt, -1);}scanf("%d", &Q);while(Q--) {int x;cnt = 0;scanf("%d", &x);query(TO[x]);ans[cnt] = 0;printf("%s\n", ans);}}return 0;
}