fork(5) download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. pair<int, int> p[2503];
  7.  
  8. int women[501][501], men[501][501]; /// is women ke liye ye men kitna pe hai?
  9.  
  10. bool cmp(pair<int, int>& p1, pair<int, int>& p2){
  11. int x = women[p1.second][p1.first], y = men[p1.first][p1.second], x1 = women[p2.second][p2.first], y1 = men[p2.first][p2.second];
  12. if(x > y) swap(x, y);
  13. if(x1 > y1) swap(x1, y1);
  14. if(y <= y1) return 1;
  15. return 0;
  16. }
  17.  
  18. void build(int n){
  19. int cnt = 0;
  20. for(int j = 1; j <= n; j++)
  21. for(int k = 1; k <= n; k++)
  22. p[cnt++] = make_pair(j, k);
  23. }
  24.  
  25. int main(){
  26. int t;
  27. cin >> t;
  28. while(t--){
  29. int n;
  30. cin >> n;
  31. for(int j = 0; j < n; j++){
  32. int ind;
  33. cin >> ind;
  34. for(int k = 1; k <= n; k++){
  35. int pos;
  36. cin >> pos;
  37. women[ind][pos] = k;
  38. }
  39. }
  40.  
  41. for(int j = 0; j < n; j++){
  42. int ind;
  43. cin >> ind;
  44. for(int k = 1; k <= n; k++){
  45. int pos;
  46. cin >> pos;
  47. men[ind][pos] = k;
  48. //if(k == n) cout << "here!\n";
  49. }
  50. }
  51. //cout << "here!\n";
  52. build(n);
  53. sort(p, p+n*n, cmp);
  54.  
  55. int already1[501] = {0}, already2[501] = {0};
  56. for(int j = 0; j < n*n; j++)
  57. if(!already1[p[j].first] && !already2[p[j].second]){
  58. already1[p[j].first] = already2[p[j].second] = 1;
  59. cout << p[j].first << " " << p[j].second << endl;
  60. }
  61. }
  62. return 0;
  63. }
  64.  
Runtime error #stdin #stdout 0s 4352KB
stdin
2
4
1 4 3 1 2
2 2 1 3 4
3 1 3 4 2
4 4 3 1 2
1 3 2 4 1
2 2 3 1 4
3 3 1 2 4
4 3 2 4 1
7
1 3 4 2 1 6 7 5
2 6 4 2 3 5 1 7
3 6 3 5 7 2 4 1
4 1 6 3 2 4 7 5
5 1 6 5 3 4 7 2
6 1 7 3 4 5 6 2
7 5 6 2 4 3 7 1
1 4 5 3 7 2 6 1
2 5 6 4 7 3 2 1
3 1 6 5 4 3 7 2
4 3 5 6 7 2 4 1
5 1 7 6 4 3 5 2
6 6 3 7 5 2 4 1
7 1 7 4 2 6 5 3
stdout
2 2
1 3
3 1
4 4