#include <stdio.h> #include <stdlib.h> #include <stdbool.h> struct stc { int index; int answer; struct stc* next; }; struct stc *ptr_array[100005]={NULL}; struct stc *ptr_now[100005]={NULL}; int priority[100005]={0}; char name[100005][20]; int visited[100005]={0}; int visited2[100005]={0}; int ans[100005]={0}; int N,Q,S,R; bool find_route(int x,int priority_now) { struct stc *pointer; pointer = ptr_array[x]; priority[priority_now]=x; visited[x]=1; if (x==R) { return true; } while (pointer!=NULL) { bool suc=false; if (visited[pointer->index]==0) suc=find_route(pointer->index,priority_now+1); if (suc) return true; pointer = pointer->next; } return false; } void find_ans(int x,int answer) { ans[x] = answer; struct stc *pointer; pointer = ptr_array[x]; visited2[x]=1; while (pointer!=NULL) { if (visited2[pointer->index]==0) find_ans(pointer->index,answer); pointer = pointer->next; } return; } int main() { scanf("%d%d%d%d",&N,&Q,&S,&R); for (int i=0;i<N;i++) { int numberrr; scanf("%d",&numberrr); scanf("%s",name[numberrr]); } for (int i=0;i<N-1;i++) { int a,b; scanf("%d%d",&a,&b); if (ptr_array[a]==NULL) { ptr_array[a] = (struct stc*)malloc(sizeof(struct stc)); ptr_array[a]->index = b; ptr_array[a]->answer = 0; ptr_array[a]->next = NULL; ptr_now[a] = ptr_array[a]; } else { ptr_now[a]->next = (struct stc*)malloc(sizeof(struct stc)); ptr_now[a]->next->index = b; ptr_now[a]->next->answer = 0; ptr_now[a]->next->next = NULL; ptr_now[a]=ptr_now[a]->next; } if (ptr_array[b]==NULL) { ptr_array[b] = (struct stc*)malloc(sizeof(struct stc)); ptr_array[b]->index = a; ptr_array[b]->answer = 0; ptr_array[b]->next = NULL; ptr_now[b] = ptr_array[b]; } else { ptr_now[b]->next = (struct stc*)malloc(sizeof(struct stc)); ptr_now[b]->next->index = a; ptr_now[b]->next->answer = 0; ptr_now[b]->next->next = NULL; ptr_now[b]=ptr_now[b]->next; } } find_route(S, 0); int i=0; while (true) { if (priority[i]==0) break; visited2[priority[i]] = 1; if (priority[i]==R) break; i++; } i=0; while (true) { if (priority[i]==0) break; find_ans(priority[i], priority[i]); if (priority[i]==R) break; i++; } while (Q--) { int query; scanf("%d",&query); printf("%s\n",name[ans[query]]); } return 0; }