Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- #pragma GCC optimize("Ofast")
- #pragma GCC optimize("O3")
- #pragma GCC optimize("unroll-loops")
- #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,tune=native")
- */
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <cmath>
- #include <map>
- #include <algorithm>
- #include <string>
- #include <utility>
- #include <set>
- #include <stack>
- #include <deque>
- #include <ctime>
- #include <random>
- #include <cassert>
- #include <cmath>
- #include <climits>
- #include <queue>
- #include <cstring>
- #include <bitset>
- #include <iomanip>
- #include <chrono>
- #ifdef LOCAL
- #define dbg(x) cerr << #x << " : " << x << endl;
- #else
- #define dbg(x)
- #endif
- // #define int long long
- #define pb push_back
- #define ppb pop_back()
- #define mp make_pair
- #define lb lower_bound
- #define ub upper_bound
- #define all(x) x.begin(), x.end()
- #define sz(a) (int)a.size()
- #define siz(a) (int)a.size()
- #define fr first
- #define se second
- #define cinv(v) for (auto& x: v) cin >> x
- #define fi(a, b) for (int i = a; i < b; ++i)
- #define fj(a, b) for (int j = a; j < b; ++j)
- #define fk(a, b) for (int k = a; k < b; ++k)
- #define fx(x, v) for (auto &x : v)
- template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
- template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
- #define mine chkmin
- #define maxe chkmax
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef char ch;
- typedef string str;
- typedef pair<int, int> pii;
- typedef vector<int> vi;
- typedef vector<vi> vvi;
- typedef vector<pii> vpii;
- typedef vector<vpii> vvpii;
- typedef vector<ch> vch;
- typedef vector<str> vs;
- const int MOD = (int)1e9 + 7;
- const int INF = (int)1e9 + 50;
- const long long BIG = (long long)2e18 + 50;
- const int MX = 200010;
- const double EPS = 1e-9;
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
- template<typename T> ostream& operator<< (ostream &out, const vector<T> &b) {
- for (auto k : b) out << k << ' ';
- return out;
- }
- template<typename T> istream& operator>> (istream &in, vector<T> &b) {
- for (auto& k : b) in >> k;
- return in;
- }
- istream& operator>> (istream& in, pii& b) {
- in >> b.first >> b.second;
- return in;
- }
- ostream& operator<< (ostream& out, const pii& b) {
- out << "{" << b.first << ", " << b.second << "}";
- return out;
- }
- const int MAXN = 1001;
- const int MAXM = MAXN * MAXN / 2;
- int n, m;
- char a[MAXN][MAXN];
- int ind[MAXN][MAXN];
- vector<int> g[MAXM];
- vector<int> b[MAXN][MAXN];
- int ban[MAXM];
- int used[MAXM];
- int ansind[MAXN][MAXN];
- char ans[MAXN][MAXN];
- vector<int> g1[MAXM];
- bool cycle = 0;
- int num = 0;
- int take[MAXM];
- void dfs(int v, int par = -1) {
- used[v] = 1;
- num ^= 1;
- for (auto u : g[v]) {
- if (u != par) {
- if (used[u]) {
- cycle = 1;
- if (num == 1) return;
- } else {
- dfs(u, v);
- }
- }
- }
- num ^= 1;
- if (num) {
- take[v] = 1;
- }
- }
- int color[MAXM];
- int mex[11];
- void dfs1(int v) {
- used[v] = 2;
- for (int i = 0; i < 11; i++) {
- mex[i] = 0;
- }
- for (auto u : g1[v]) {
- if (used[u] < 2) {
- dfs1(u);
- }
- mex[color[u]] = 1;
- }
- for (int i = 0; i < 11; i++) {
- if (mex[i] == 0) {
- color[v] = i;
- break;
- }
- }
- }
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin >> n >> m;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- cin >> a[i][j];
- ind[i][j] = -1;
- }
- }
- int lst = 0;
- for (int i = 0; i < n; i++) {
- for (int j = 1; j < m; j++) {
- if (a[i][j] != '.' && a[i][j] == a[i][j - 1]) {
- ind[i][j] = ind[i][j - 1] = lst++;
- if (a[i][j] >= 'a' && a[i][j] <= 'z') {
- if (i > 0) {
- b[i - 1][j].pb(lst - 1);
- b[i - 1][j - 1].pb(lst - 1);
- } else {
- ban[lst - 1] = 1;
- }
- } else {
- if (i == n - 1) ban[lst - 1] = 1;
- else {
- b[i + 1][j].pb(lst - 1);
- b[i + 1][j - 1].pb(lst - 1);
- }
- }
- }
- }
- }
- for (int i = 1; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (a[i][j] == a[i - 1][j] && a[i][j] != '.') {
- ind[i][j] = ind[i - 1][j] = lst++;
- if (a[i][j] >= 'a' && a[i][j] <= 'z') {
- if (j == 0) ban[lst - 1] = 1;
- else {
- b[i][j - 1].pb(lst - 1);
- b[i - 1][j - 1].pb(lst - 1);
- }
- } else {
- if (j == m - 1) ban[lst - 1] = 1;
- else {
- b[i][j + 1].pb(lst - 1);
- b[i - 1][j + 1].pb(lst - 1);
- }
- }
- }
- }
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (a[i][j] != '.') {
- for (auto k : b[i][j]) {
- ban[k] = 1;
- }
- }
- }
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (a[i][j] == '.') {
- int sz = b[i][j].size();
- for (int i1 = 0; i1 < sz; i1++) {
- for (int j1 = i1 + 1; j1 < sz; j1++) {
- g[b[i][j][i1]].pb(b[i][j][j1]);
- g[b[i][j][j1]].pb(b[i][j][i1]);
- }
- }
- }
- }
- }
- for (int i = 0; i < lst; i++) {
- sort(all(g[i]));
- g[i].resize(unique(all(g[i])) - g[i].begin());
- vector<int> nw;
- for (auto k : g[i]) {
- if (ban[i] || ban[k]) {
- continue;
- }
- nw.pb(k);
- }
- g[i] = nw;
- }
- for (int i = 0; i < lst; i++) {
- if (!used[i] && !ban[i]) {
- cycle = 0;
- num = 0;
- dfs(i);
- }
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- ansind[i][j] = ind[i][j];
- }
- }
- for (int i = 0; i < n; i++) {
- for (int j = 1; j < m; j++) {
- if (ind[i][j] != -1 && take[ind[i][j]] && ind[i][j - 1] == ind[i][j]) {
- if (a[i][j] >= 'a' && a[i][j] <= 'z') {
- ansind[i - 1][j] = ind[i][j];
- ansind[i - 1][j - 1] = ind[i][j];
- } else {
- ansind[i + 1][j] = ind[i][j];
- ansind[i + 1][j - 1] = ind[i][j];
- }
- }
- }
- }
- for (int i = 1; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (ind[i][j] != -1 && take[ind[i][j]] && ind[i][j] == ind[i - 1][j]) {
- if (a[i][j] >= 'a' && a[i][j] <= 'z') {
- ansind[i][j - 1] = ansind[i - 1][j - 1] = ind[i][j];
- } else {
- ansind[i][j + 1] = ansind[i - 1][j + 1] = ind[i][j];
- }
- }
- }
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- // cout << ansind[i][j] << " ";
- }
- // cout << "\n";
- }
- for (int i = 0; i < n; i++) {
- for (int j = 1; j < m; j++) {
- int ind1 = ansind[i][j], ind2 = ansind[i][j - 1];
- if (ind1 != ind2 && ind1 != -1 && ind2 != -1) {
- g1[ind1].pb(ind2);
- g1[ind2].pb(ind1);
- }
- }
- }
- for (int i = 1; i < n; i++) {
- for (int j = 0; j < m; j++) {
- int ind1 = ansind[i][j], ind2 = ansind[i - 1][j];
- if (ind1 != ind2 && ind1 != -1 && ind2 != -1) {
- g1[ind1].pb(ind2);
- g1[ind2].pb(ind1);
- }
- }
- }
- for (int i = 0; i < lst; i++) {
- if (used[i] < 2) dfs1(i);
- }
- int ansval = 0;
- for (int i = 0; i < lst; i++) {
- ansval += take[i];
- }
- cout << ansval << "\n";
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (ansind[i][j] == -1) {
- cout << ".";
- } else {
- cout << color[ansind[i][j]];
- }
- }
- cout << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement