sacgajcvs

Untitled

Oct 11th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define pb push_back
  4. #define ppb pop_back
  5. #define endl '\n'
  6. #define mii map<ll,ll>
  7. #define msi map<string,ll>
  8. #define mis map<ll, string>
  9. #define rep(i,a,b) for(ll i=a;i<b;i++)
  10. #define repr(i,a,b) for(ll i=b-1;i>=a;i--)
  11. #define trav(a, x) for(auto& a : x)
  12. #define pii pair<ll,ll>
  13. #define vi vector<ll>
  14. #define vii vector<pair<ll, ll>>
  15. #define vs vector<string>
  16. #define all(a) (a).begin(),(a).end()
  17. #define F first
  18. #define S second
  19. #define sz(x) (ll)x.size()
  20. #define hell 1000000007
  21. #define lbnd lower_bound
  22. #define ubnd upper_bound
  23. #define max(a,b) (a>b?a:b)
  24. #define min(a,b) (a<b?a:b)
  25.  
  26.  
  27. std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
  28. #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
  29. #define TIME cerr << "\nTime elapsed: " << setprecision(5) <<1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
  30. #define DECIMAL(n) cout << fixed ; cout << setprecision(n);
  31. #define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  32. using namespace std;
  33. #define PI 3.141592653589793
  34. #define N 100005
  35.  
  36. void solve()
  37. {
  38. ll n;
  39. cin >> n;
  40. vector<vi> v(n,vi(n));
  41. rep(i,0,n) {
  42. rep(j,0,n) {
  43. cin >> v[i][j];
  44. }
  45. }
  46. vector<vi> tmp(n,vi(n,0));
  47. map<ll,set<ll>> mar[n],mac[n];
  48. rep(i,0,n) {
  49. rep(j,0,n) {
  50. mar[i][v[i][j]].insert(j);
  51. }
  52. trav(j,mar[i]) {
  53. if(sz(j.S)>1) {
  54. trav(k,j.S) {
  55. tmp[i][k]++;
  56. }
  57. }
  58. }
  59. }
  60. rep(i,0,n) {
  61. rep(j,0,n) {
  62. mac[i][v[j][i]].insert(j);
  63. }
  64. trav(j,mac[i]) {
  65. if(sz(j.S)>1) {
  66. trav(k,j.S) {
  67. tmp[k][i]++;
  68. }
  69. }
  70. }
  71. }
  72. ll cnt=0;
  73.  
  74. rep(i,0,n) {
  75. rep(j,0,n) {
  76. if(tmp[i][j]>1) {
  77. cnt++;
  78. mar[i][v[i][j]].erase(j);
  79. mac[j][v[i][j]].erase(i);
  80. tmp[i][j] = 0;
  81. if(sz(mar[i][v[i][j]])==1) {
  82. tmp[i][*mar[i][v[i][j]].begin()] = 0;
  83. }
  84. if(sz(mac[j][v[i][j]])==1) {
  85. tmp[*mac[j][v[i][j]].begin()][j] = 0;
  86. }
  87. }
  88. }
  89. }
  90.  
  91. rep(i,0,n) {
  92. rep(j,0,n) {
  93. if(tmp[i][j]>0) {
  94. cnt++;
  95. if(sz(mar[i][v[i][j]])>1) {
  96. tmp[i][j] = 0;
  97. mar[i][v[i][j]].erase(j);
  98. if(sz(mar[i][v[i][j]])==1) {
  99. tmp[i][*mar[i][v[i][j]].begin()] = 0;
  100. }
  101. } else {
  102. tmp[i][j] = 0;
  103. mac[j][v[i][j]].erase(i);
  104. if(sz(mac[j][v[i][j]])==1) {
  105. tmp[*mac[j][v[i][j]].begin()][j] = 0;
  106. }
  107. }
  108.  
  109. }
  110. }
  111. }
  112.  
  113. cout << cnt << endl;
  114. return;
  115. }
  116. int main()
  117. {
  118. FAST
  119. int TESTS=1;
  120. cin>>TESTS;
  121. rep(i,0,TESTS)
  122. {
  123. // cout<<"Case #"<<i+1<<": ";
  124. solve();
  125. }
  126. TIME
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment