fork(1) 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. //cout<<p1.first<<" "<<p1.second<<" "<<p2.first<<" "<<p2.second<<endl;
  12. 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];
  13. if(x > y) swap(x, y);
  14. if(x1 > y1) swap(x1, y1);
  15. if(y <= y1) return 1;
  16. return 0;
  17. }
  18.  
  19. void build(int n){
  20. int cnt = 0;
  21. for(int j = 1; j <= n; j++)
  22. for(int k = 1; k <= n; k++)
  23. p[cnt++] = make_pair(j, k);
  24. }
  25.  
  26. int main(){
  27. int t;
  28. cin >> t;
  29. while(t--){
  30. int n;
  31. cin >> n;
  32. for(int j = 0; j < n; j++){
  33. int ind;
  34. cin >> ind;
  35. for(int k = 1; k <= n; k++){
  36. int pos;
  37. cin >> pos;
  38. women[ind][pos] = k;
  39. }
  40. }
  41.  
  42. for(int j = 0; j < n; j++){
  43. int ind;
  44. cin >> ind;
  45. for(int k = 1; k <= n; k++){
  46. int pos;
  47. cin >> pos;
  48. men[ind][pos] = k;
  49. //if(k == n) cout << "here!\n";
  50. }
  51. }
  52. //cout << "here!\n";
  53. build(n);
  54. cout<<"Builder working fine"<<endl;
  55. sort(p, p+n*n, cmp);
  56. cout<<"Sort working fine"<<endl;
  57.  
  58. int already1[501] = {0}, already2[501] = {0};
  59. for(int j = 0; j < n*n; j++)
  60. if(!already1[p[j].first] && !already2[p[j].second]){
  61. already1[p[j].first] = already2[p[j].second] = 1;
  62. cout << p[j].first << " " << p[j].second << endl;
  63. }
  64. }
  65. return 0;
  66. }
  67.  
Runtime error #stdin #stdout 0s 4388KB
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
Builder working fine
Sort working fine
2 2
1 3
3 1
4 4
Builder working fine