fork download
#include <bits/stdc++.h>
using namespace std;

bool findB(int x, int y, int b[], int m){
    for(int i = 0; i < m; i++){
        if(x == b[i] || y == b[i]) return true;
    }
    return false;
}

bool f(int n, int m, int x[], int y[]){
    bool changed = true;
    while(changed) {
        changed = false;
        for(int i = n - 2; i >= 0; i--){
            if(x[i] > x[i+1]){
                if(findB(x[i], x[i+1], y, m)){
                    swap(x[i], x[i+1]);
                    changed = true;
                } else {
                    return false;
                }
            }
        }
    }
    return true;
}


int main(){
    int t; cin >> t;
    while(t--){
        int n, m; cin >> n >> m;
        int a[n], b[m];
        for (int i = 0; i < n; i++) cin >> a[i];
        for (int i = 0; i < m; i++) cin >> b[i];
        if(f(n, m, a, b)){
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Standard output is empty