我正在尝试使用列表和数组来实现稳定的婚姻。当我尝试在codechef上提交它时,它会给运行时超出错误。如果有人可以提供帮助,但我想使用我正在使用的相同数据结构。
import java.util.*;
public class Main {
private int size;
private static LinkedList<Integer>[] menlist;
private static int[][] womatrix;
private static int[][] invwomatrix;
private static int[] womatch;
private static int[] menmatch;
public static void main(String[] args){
Scanner Input = new Scanner(System.in);
int TestCases = Input.nextInt();
while(TestCases-- > 0) {
int size = Input.nextInt();
womatrix=new int[size][size];
invwomatrix=new int[size][size];
menlist=new LinkedList[size];
womatch=new int[size];
menmatch=new int[size];
for(int i = 0; i < size; i++){
int temp=Input.nextInt();
for(int j = 0; j < size; j++)
{
womatrix[i][j] = Input.nextInt();
invwomatrix[i][womatrix[i][j]-1]=j+1;
}
}
for(int i = 0; i < size; i++){
int temp=Input.nextInt();
LinkedList<Integer> menpref = new LinkedList<Integer>();
for(int j = 0; j < size; j++)
{
int a=Input.nextInt();
menpref.add(a);
}
menlist[i]=menpref;
}
LinkedList<Integer> ll = new LinkedList<Integer>();
for(int i=0;i<size;i++){
womatch[i]=0;
menmatch[i]=0;
ll.add(i+1);
}
while((ll.isEmpty()!=true) || menlist[ll.getFirst()-1].isEmpty()!=true ){
int newman=ll.removeFirst();
//System.out.print("freeman "+newman+" "+menlist[newman-1].isEmpty()+"\n");
int women=(menlist[newman-1].removeFirst());
//System.out.print("highrank "+women+"intial_pair "+womatch[women-1]+"\n");
if(womatch[women-1]==0){
womatch[women-1]=newman;
menmatch[newman-1]=women;
//int a=ll.removeFirst();
}
else{
int oldman=womatch[women-1];
if(invwomatrix[women-1][newman-1]<invwomatrix[women-1][oldman-1]){
womatch[women-1]=newman;
menmatch[newman-1]=women;
ll.addFirst(oldman);
}
else{
ll.addFirst(newman);
}
}
/*for(int i=0; i<this.size; i++) {
System.out.print((i+1)+" "+ menmatch[i] +"\n");
}*/
if(ll.isEmpty()==true){break;}
}
for(int k=0;k<size;k++){
System.out.print((k+1)+" "+menmatch[k] +"\n");
}
}
}
}