Problem:
Problem H: Testing Sorting Networks

A N sorting network is a circuit that accepts N
numbers as its input, outputs them sorted. Mr. Smith is an engineer of a company
that sells various sizes of the circuit.

One day, the company got an order of N sorting networks.
Unfortunately, they didn't have the circuit for N numbers at the
time. The clerk once declined the order for being out of stock, but the client
was so urgent that he offered much money if he could get the circuit in a week.
The deal escalated to a manager, and she asked Mr. Smith for a solution to
produce the N sorting networks by the deadline.

He came up with an idea to combine several N/2 sorting
networks, because he noticed that they have many stocks of the circuits for N/2
numbers. He designed a new circuit using the N/2 sorting networks,
but he was not sure if it would really work as an N sorting network.
So, he asked a colleague, you, to check if it was actually an N
sorting network.

The circuit he designed consists of multiple stages. Each stage of the
circuit consists of two N/2 sorting networks, which makes each stage
accept a sequence of N numbers as inputs and output a sequence of N
numbers. From the 1st to the N/2-th input numbers of a stage goes to
one of those N/2 sorting networks, and from the (N/2+1)-th
to the N-th input numbers goes to the other. Similarly, the first
half of the output numbers of a stage is the output of the former sorting
network and the second half is the output of the latter, both of which are
sorted in ascending order. Each output of a stage is connected to exactly one
input of the next stage, and no two inputs are connected to the same output
line. The input of the last stage is the input of the whole circuit and the
output of the first stage is the output of the whole circuit.

Input

The input begins with a line containing a positive even integer N
(4 <= N <= 100) and a positive integer D (1 <= D
<= 10). N indicates the number of the input and output of the circuit
and D indicates the number of the stages of the circuit. The i-th
line of the following D-1 lines contains N integers wi1,
wi2,..., wiN
(1 <= wij <= N ), which describes
the wiring between the i-th and (i+1)-th stages. wij
indicates the j-th input of the i-th stage and the wij-th
output of the (i+1)-th stage are wired. You can assume wi1,
wi2,..., wiN
are unique for each i.

Output

Print a line containing "Yes" if the circuit works as an N
sorting network. Print "No" otherwise.

Sample Input 1

4 4
1 3 4 2
1 4 2 3
1 3 2 4

Output for the Sample Input 1

Yes

Sample Input 2

4 3
3 1 2 4
1 3 2 4

Output for the Sample Input 2

No

Solution in JAVA (judge verdict: Wrong Answer)

import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;

public class Main{
	Scanner sc=new Scanner(System.in);

	int INF=1<<28;
	double EPS=1e-12;

	int n, d;
	int[][] w;

	void run(){
		n=sc.nextInt();
		d=sc.nextInt();
		w=new int[d-1][n];
		for(int j=0; j<d-1; j++){
			for(int i=0; i<n; i++){
				w[j][i]=sc.nextInt()-1;
			}
		}
		solve();
	}

	void solve(){
		int[] a=new int[n], b=new int[n];
		boolean ok=true;
		for(int n0a=0; n0a<=n/2; n0a++){
			for(int n0b=0; n0b<=n/2; n0b++){
				int n1a=n/2-n0a, n1b=n/2-n0b;
				fill(a, 1);
				for(int i=0; i<n0a; i++){
					a[i]=0;
				}
				for(int i=0; i<n0b; i++){
					a[i+n/2]=0;
				}
//				debug("1", a);
				for(int j=0; j<d-1; j++){
					for(int i=0; i<n; i++){
						// b[w[j][i]]=a[i];
						b[i]=a[w[j][i]];
					}
					System.arraycopy(b, 0, a, 0, n);
					sort(a, 0, n/2);
					sort(a, n/2, n);
				}
//				debug("2", a);
				for(int i=0; i<n-1; i++){
					ok&=a[i]<=a[i+1];
				}
			}
		}
		println(ok?"Yes":"No");
	}

	void debug(Object... os){
		System.err.println(Arrays.deepToString(os));
	}

	void print(String s){
		System.out.print(s);
	}

	void println(String s){
		System.out.println(s);
	}

	public static void main(String[] args){
		new Main().run();
	}
}