#/*********************************************************** # marriage.rb -- 安定な結婚の問題 #***********************************************************/ N = 3 # 各性の人数 $line = [] def scanf if $line == [] l = gets return nil if l == nil $line = l.split end $line.shift end row = []; (N+1).times { row.push(0) } boy = [] girl = []; (N+1).times { girl.push(row.dup) } position = [] rank = []; (N+1).times { rank.push(row.dup) } for g in 1..N # 各女性の好み for r in 1..N b = scanf.to_i; rank[g][b] = r end boy[g] = 0; rank[g][0] = N + 1; # 番人 end for b in 1..N # 各男性の好み for r in 1..N; girl[b][r] = scanf.to_i; end position[b] = 0 end for b in 1..N s = b while (s != 0) g = girl[s][position[s] += 1] if (rank[g][s] < rank[g][boy[g]]) t = boy[g]; boy[g] = s; s = t end end end for g in 1..N printf("女 %d - 男 %d\n", g, boy[g]) end exit 0