package chelper; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import io.InputReader; import io.OutputWriter; public class F { class Point { final int x; final int y; public Point(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Point point = (Point) o; if (x != point.x) return false; return y == point.y; } @Override public int hashCode() { int result = x; result = 31 * result + y; return result; } public Point sub(Point p) { return new Point(x - p.x, y - p.y); } public Point add(Point p) { return new Point(x + p.x, y + p.y); } @Override public String toString() { return x + " " + y; } } public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int m = in.nextInt(); boolean[][] a = new boolean[n][m]; Map locations = new HashMap<>(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int t = in.nextInt(); locations.put(t, new Point(i, j)); } } for (int i = 0; i < n * m; i++) { int t = in.nextInt(); Point p = locations.get(t); int x = p.x; int y = p.y; if (x < n - 1) { if (!a[x + 1][y]) { out.println("NO"); return; } } a[x][y] = true; } out.println("YES"); } }