Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 3 4
- 1 5 7 8
- 2 4 1 5
- 1 1 1 1
- */
- /*
- _____ _ _ _ _
- |_ _| |__ ___ / \ _ __ ___| |__ _ _| |
- | | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | |
- | | | | | | __// ___ \| | | \__ \ | | | |_| | |
- |_| |_| |_|\___/_/ \_\_| |_|___/_| |_|\__,_|_|
- */
- #include<bits/stdc++.h>
- #define ll long long
- #define pb push_back
- #define ppb pop_back
- #define endl '\n'
- #define mii map<ll int,ll int>
- #define msi map<string,ll int>
- #define mis map<ll int, string>
- #define rep(i,a,b) for(ll int i=a;i<b;i++)
- #define mpi map<pair<ll int,ll int>,ll int>
- #define pii pair<ll int,ll int>
- #define vi vector<ll int>
- #define vii vector<pair<ll int, ll int>>
- #define vs vector<string>
- #define all(a) (a).begin(),(a).end()
- #define F first
- #define S second
- #define sz(x) (ll int)x.size()
- #define hell 1000000007
- #define lbnd lower_bound
- #define ubnd upper_bound
- #define bs binary_search
- #define mp make_pair
- #define what_is(x) cerr << #x << " is " << x << endl;
- #define time cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
- using namespace std;
- #define N 100005
- /*
- Instructions :
- 1. Set p each i of p eqaul to i initailly.
- 2. rank is initiialised with 0.
- 3. To know parent run a loop for each value call find function.
- */
- ll ans[N];
- ll rank[N];
- ll p[N]; // set p of i equal to i initially
- ll find(ll x)
- {
- if(p[x]!=x)
- p[x]=find(p[x]);
- return p[x];
- }
- void merge(ll x,ll y)
- {
- ll Px,Py;
- Px=find(x);
- Py=find(y);
- if(::rank[Px]>::rank[Py])
- p[Py]=Px;
- else
- p[Px]=Py;
- if(::rank[Px]==::rank[Py])
- ::rank[Py]++;
- }
- void solve()
- {
- ll n,m;
- cin>>n>>m;
- rep(i,0,n*m+1)
- {
- p[i]=i;
- }
- ll a[n][m];
- rep(i,0,n)
- {
- rep(j,0,m)
- {
- cin>>a[i][j];
- }
- }
- vi row[n],col[m];
- map<ll,vii>pm;
- rep(i,0,n)
- {
- map<ll,ll>tmp;
- rep(j,0,m)
- {
- pm[a[i][j]].pb({i,j});
- if(tmp[a[i][j]])
- {
- merge(tmp[a[i][j]],i*m+j+1);
- }
- else
- {
- tmp[a[i][j]]=i*m+j+1;
- }
- }
- }
- rep(j,0,m)
- {
- map<ll,ll>tmp;
- rep(i,0,n)
- {
- if(tmp[a[i][j]])
- {
- merge(tmp[a[i][j]],i*m+j+1);
- }
- else
- {
- tmp[a[i][j]]=i*m+j+1;
- }
- }
- }
- for(auto i:pm)
- {
- for(auto j:i.S)
- {
- ll par=find(j.F*m+j.S+1);
- if(sz(row[j.F])!=0)
- {
- ans[par]=max(ans[par],row[j.F].back()+1);
- }
- if(sz(col[j.S])!=0)
- {
- ans[par]=max(ans[par],col[j.S].back()+1);
- }
- }
- for(auto j:i.S)
- {
- ll par=find(j.F*m+j.S+1);
- row[j.F].pb(ans[par]);
- col[j.S].pb(ans[par]);
- }
- }
- rep(i,0,n)
- {
- rep(j,0,m)
- {
- cout<<ans[find(i*m+j+1)]+1<<" ";
- }
- cout<<endl;
- }
- return;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- int TESTS=1;
- // cin>>TESTS;
- while(TESTS--)
- {
- solve();
- }
- time
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement