-
Notifications
You must be signed in to change notification settings - Fork 0
/
StableMarriage.java
78 lines (76 loc) · 2.08 KB
/
StableMarriage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package ques;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
public class StableMarriage {
public static void main(String[] args) throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t>0){
t--;
int n=Integer.parseInt(br.readLine());
HashMap<Integer, ArrayList<Integer>> girlpref=new HashMap<Integer, ArrayList<Integer>>();
HashMap<Integer, ArrayList<Integer>> boypref=new HashMap<Integer, ArrayList<Integer>>();
for(int i=0;i<n;i++){
String[] s=br.readLine().split(" ");
int x=Integer.parseInt(s[0]);
ArrayList<Integer> arr=new ArrayList<Integer>();
for(int j=1;j<s.length;j++){
arr.add(Integer.parseInt(s[j]));
}
girlpref.put(x,arr);
}
for(int i=0;i<n;i++){
String[] s=br.readLine().split(" ");
int x=Integer.parseInt(s[0]);
ArrayList<Integer> arr=new ArrayList<Integer>();
for(int j=1;j<s.length;j++){
arr.add(Integer.parseInt(s[j]));
}
boypref.put(x,arr);
}
boolean[] g=new boolean[n];
ArrayList<Integer> ar=new ArrayList<Integer>();
for(int i=0;i<=n;i++){
ar.add(i);
}
HashMap<Integer,Integer> hm=new HashMap<Integer, Integer>();
ArrayList<Integer> arc=new ArrayList<Integer>();
for(int i=1;i<=n;i++){
arc.add(i);
}
HashMap<Integer, Integer> hmm=new HashMap<Integer, Integer>();
while(arc.size()>0){
int i=arc.get(0);
ArrayList<Integer> temp=boypref.get(arc.get(0));
for(int j=0;j<temp.size();j++){
int x=temp.get(j);
if(!g[x-1]){
g[x-1]=true;
hm.put(x, i);
hmm.put(i, x);
arc.remove(0);
break;
}
else{
ArrayList<Integer> temp2=new ArrayList<Integer>();
temp2=girlpref.get(x);
int p=hm.get(x);
if(temp2.indexOf(i)<temp2.indexOf(p)){
hm.put(x, i);
hmm.remove(p);
hmm.put(i, x);
arc.remove(0);
arc.add(p);
break;
}
}
}
}
for(int i=1;i<=n;i++){
System.out.println(i+" "+hmm.get(i));
}
}
}
}