#include <cstdio>
#include <cstdlib>
using namespace std;
struct Tree{
int v;
Tree *left,*right;
}* Tree_A[1099],* Tree_B[1099];
int n,m;
int L,R;
void Build_A(Tree *thead,int num,int v){
thead->v=v;
if(num==0){
thead->left=NULL;
thead->right=NULL;
}
if(num==1){
Tree_A[L] =thead->left=(Tree *)malloc(sizeof(Tree));
thead->right=NULL;
}
if(num==2){
Tree_A[L] = thead->left=(Tree *)malloc(sizeof(Tree));
Tree_A[R] = thead->right=(Tree *)malloc(sizeof(Tree));
}
}
void Build_B(Tree *thead,int num,int v){
thead->v=v;
if(num==0){
thead->left=NULL;
thead->right=NULL;
}
if(num==1){
Tree_B[L] =thead->left=(Tree *)malloc(sizeof(Tree));
thead->right=NULL;
}
if(num==2){
Tree_B[L] = thead->left=(Tree *)malloc(sizeof(Tree));
Tree_B[R] = thead->right=(Tree *)malloc(sizeof(Tree));
}
}
int AtreeV[1099],BtreeV[1099];
int dif=0;
int dfs(Tree* Athead,Tree *Bthead){
if(dif==1)return 1;
if(Bthead->left!=NULL){
if(Athead->left==NULL||(Athead->left->v)!=(Bthead->left->v)){
dif=1;
return 1;
}
dfs(Athead->left,Bthead->left);
}
if(Bthead->right!=NULL){
if(Athead->right==NULL||(Athead->right->v)!=(Bthead->right->v)){
dif=1;
return 1;
}
dfs(Athead->right,Bthead->right);
}
if(dif==1)return 1;
else return 0;
}
int find(){
int i,j;
for(i=1;i<=n;i++){
if((Tree_A[i]->v)!=(Tree_B[1]->v))continue;
dif=0;
if(dfs(Tree_A[i],Tree_B[1])==0) return 1;
}
return 0;
}
int main(){
int i,j,k,num,no;
while(scanf("%d%d",&n,&m)!=EOF){
for(i=1;i<=n;i++){
scanf("%d",&AtreeV[i]);
}
Tree_A[1]=(Tree *)malloc(sizeof(Tree));
for(i=1;i<=n;i++){
scanf("%d",&num);
if(num==1){
scanf("%d",&L);
}
if(num==2){
scanf("%d%d",&L,&R);
}Build_A(Tree_A[i],num,AtreeV[i]);
}
for(i=1;i<=m;i++){
scanf("%d",&BtreeV[i]);
}
Tree_B[1]=(Tree *)malloc(sizeof(Tree));
for(i=1;i<=m;i++){
scanf("%d",&num);
if(num==1){
scanf("%d",&L);
}
if(num==2){
scanf("%d%d",&L,&R);
}Build_B(Tree_B[i],num,BtreeV[i]);
}
if(m==0||find()==0){
printf("NO\n");
}else{
printf("YES\n");
}
}
return 0;
}
/**************************************************************
Problem: 1520
User: 萧然677
Language: C++
Result: Accepted
Time:10 ms
Memory:1180 kb
****************************************************************/
代码昨晚睡前看了网上的,http://www.cnblogs.com/huhuuu/p/3384978.html