#include <bits/stdc++.h> using namespace std; queue<int> dog,like[1005]; int rec[1005][1005],hus[1005],wif[1005]; bool marry(int woman,int man){ if(!hus[woman]){ hus[woman]=man; wif[man]=woman; return true; } if(rec[woman][hus[woman]]<rec[woman][man]){ dog.push(hus[woman]); wif[hus[woman]]=0; hus[woman]=man; wif[man]=woman; return true; } return false; } void solve(){ while(!dog.empty()){ int a=dog.front(); while(!marry(like[a].front(),a))like[a].pop(); dog.pop(); } } int main(){ int T; ios::sync_with_stdio(false); cin>>T; while(T--){ int n; cin>>n; for(int i=1;i<=n;i++){ while(!like[i].empty())like[i].pop(); for(int j=1;j<=n;j++){ int a; cin>>a; like[i].push(a); } } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ int a; cin>>a; rec[i][a]=n-j+1; } for(int i=1;i<=n;i++) dog.push(i); solve(); for(int i=1;i<=n;i++) cout<<wif[i]<<endl; } return 0; }
时间: 2024-10-11 21:14:58