Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define ll long long
- #define pb push_back
- #define ppb pop_back
- #define endl '\n'
- #define mii map<ll,ll>
- #define msi map<string,ll>
- #define mis map<ll, string>
- #define rep(i,a,b) for(ll i=a;i<b;i++)
- #define repr(i,a,b) for(ll i=b-1;i>=a;i--)
- #define trav(a, x) for(auto& a : x)
- #define pii pair<ll,ll>
- #define vi vector<ll>
- #define vii vector<pair<ll, ll>>
- #define vs vector<string>
- #define all(a) (a).begin(),(a).end()
- #define F first
- #define S second
- #define sz(x) (ll)x.size()
- #define hell 1000000007
- #define lbnd lower_bound
- #define ubnd upper_bound
- #define max(a,b) (a>b?a:b)
- #define min(a,b) (a<b?a:b)
- std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
- #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
- #define TIME cerr << "\nTime elapsed: " << setprecision(5) <<1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
- #define DECIMAL(n) cout << fixed ; cout << setprecision(n);
- #define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- using namespace std;
- #define PI 3.141592653589793
- #define N 100005
- void solve()
- {
- ll n;
- cin >> n;
- vector<vi> v(n,vi(n));
- rep(i,0,n) {
- rep(j,0,n) {
- cin >> v[i][j];
- }
- }
- vector<vi> tmp(n,vi(n,0));
- map<ll,set<ll>> mar[n],mac[n];
- rep(i,0,n) {
- rep(j,0,n) {
- mar[i][v[i][j]].insert(j);
- }
- trav(j,mar[i]) {
- if(sz(j.S)>1) {
- trav(k,j.S) {
- tmp[i][k]++;
- }
- }
- }
- }
- rep(i,0,n) {
- rep(j,0,n) {
- mac[i][v[j][i]].insert(j);
- }
- trav(j,mac[i]) {
- if(sz(j.S)>1) {
- trav(k,j.S) {
- tmp[k][i]++;
- }
- }
- }
- }
- ll cnt=0;
- rep(i,0,n) {
- rep(j,0,n) {
- if(tmp[i][j]>1) {
- cnt++;
- mar[i][v[i][j]].erase(j);
- mac[j][v[i][j]].erase(i);
- tmp[i][j] = 0;
- if(sz(mar[i][v[i][j]])==1) {
- tmp[i][*mar[i][v[i][j]].begin()] = 0;
- }
- if(sz(mac[j][v[i][j]])==1) {
- tmp[*mac[j][v[i][j]].begin()][j] = 0;
- }
- }
- }
- }
- rep(i,0,n) {
- rep(j,0,n) {
- if(tmp[i][j]>0) {
- cnt++;
- if(sz(mar[i][v[i][j]])>1) {
- tmp[i][j] = 0;
- mar[i][v[i][j]].erase(j);
- if(sz(mar[i][v[i][j]])==1) {
- tmp[i][*mar[i][v[i][j]].begin()] = 0;
- }
- } else {
- tmp[i][j] = 0;
- mac[j][v[i][j]].erase(i);
- if(sz(mac[j][v[i][j]])==1) {
- tmp[*mac[j][v[i][j]].begin()][j] = 0;
- }
- }
- }
- }
- }
- cout << cnt << endl;
- return;
- }
- int main()
- {
- FAST
- int TESTS=1;
- cin>>TESTS;
- rep(i,0,TESTS)
- {
- // cout<<"Case #"<<i+1<<": ";
- solve();
- }
- TIME
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment