This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef complex<double> cd;
const int MAXN = 5e5+10;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
int n, m;
vector<vector<set<pii>>> adj;
vector<vector<pii>> pai;
vector<vector<int>> peso, val, mark;
int d[4]={0, 0, 1, -1};
pii find(pii x){ return (pai[x.fi][x.se]==x ? x : pai[x.fi][x.se]=find(pai[x.fi][x.se])); }
void join(pii a, pii b){
a=find(a); b=find(b);
if(a==b) return;
if(peso[a.fi][a.se]>peso[b.fi][b.se]) swap(a, b);
pai[a.fi][a.se]=b;
peso[b.fi][b.se]=max(peso[b.fi][b.se], peso[a.fi][a.se]+1);
for(auto u : adj[a.fi][a.se])
adj[b.fi][b.se].insert(u);
adj[a.fi][a.se].clear();
}
void dfs(pii ver){
mark[ver.fi][ver.se]=1;
for(int i=0;i<4;i++){
int dx=ver.fi+d[i], dy=ver.se+d[3-i];
if(dx>=0 && dx<n && dy>=0 && dy<m && val[ver.fi][ver.se]==val[dx][dy] && !mark[dx][dy]){
join(ver, {dx, dy});
dfs({dx, dy});
}
}
}
void solve(){
cin >> n >> m;
pai.resize(n, vector<pii>(m)); peso.resize(n, vector<int>(m, 0)), val.resize(n, vector<int>(m));
mark.resize(n, vector<int>(m, 0)); adj.resize(n, vector<set<pii>>(m));
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
pai[i][j]={i, j};
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin >> val[i][j];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(!mark[i][j])
dfs({i, j});
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
for(int k=0;k<4;k++){
int dx=i+d[k], dy=j+d[k];
if(dx<0 || dx>=n || dy<0 || dy>=m || find({i, j})==find({dx, dy})) continue;
pii a=find({i, j}), b=find({dx, dy});
adj[a.fi][a.se].insert(b);
adj[b.fi][b.se].insert(a);
}
}
}
int q; cin >> q;
while(q--){
int x, y, c; cin >> x >> y >> c; x--; y--;
pii p=find({x, y});
if(val[p.fi][p.se]==c) continue;
vector<pii> temp;
for(auto u : adj[p.fi][p.se]){
temp.pb(u);
}
for(auto u : temp) join(p, u);
p=find(p);
val[p.fi][p.se]=c;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
pii p=find({i, j});
cout << val[p.fi][p.se] << " \n"[j==n-1];
}
}
}
int32_t main(){
//~ ios_base::sync_with_stdio(false); cin.tie(NULL);
int tt=1;
//~ cin >> tt;
while(tt--) solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |