Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
- // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
- // #pragma comment(linker, "/stack:200000000"]
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- #include <unordered_set>
- #include <unordered_map>
- #include <set>
- #include <map>
- #include <queue>
- #include <deque>
- #include <bitset>
- #include <stack>
- #include <random>
- #include <fstream>
- #include <sstream>
- #include <chrono>
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define ld long double
- #define hm unordered_map
- #define pii pair<int, int>
- #define sz(a) (int)a.size()
- #define all(a) a.begin(), a.end()
- #define cinv(v) for (auto& x: v) cin >> x
- #define fr(i, n) for (int i = 0; i < n; ++i)
- #define fl(i, l, n) for (int i = l; i < n; ++i)
- // #define int ll
- 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;}
- using namespace std;
- #ifdef LOCAL
- #define dbg(x) cerr << #x << " : " << x << '\n'
- const int maxn = 1000 + 20;
- #else
- #define dbg(x)
- const int maxn = 1000 + 20;
- #endif
- //tg: @runningcherry
- ostream& operator << (ostream& out, const pii& v) {
- out << v.fi << ", " << v.se;
- return out;
- }
- template<typename T> ostream& operator << (ostream& out, const vector<T>& v) {
- for (auto& x: v) out << x << " ";
- return out;
- }
- istream& operator >> (istream& in, pii& a) {
- in >> a.fi >> a.se;
- return in;
- }
- const ll inf = (ll) 2e9;
- const ld pi = asin (1) * 2;
- const ld eps = 1e-8;
- const ll mod = (ll)1e9 + 7;
- const ll ns = 97;
- mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
- int t[maxn * maxn], sz[maxn * maxn];
- struct DSU {
- vector<vector<int>> order;
- DSU (int n) {
- for (int i = 0; i < n; ++i) {
- t[i] = i;
- sz[i] = 1;
- }
- }
- inline void rollback () {
- auto ta = order.back ()[0], a = order.back ()[1], b = order.back ()[2], szb = order.back ()[3];
- order.pop_back ();
- t[a] = ta;
- sz[b] = szb;
- }
- inline int get (int x) {
- while (t[x] != x) x = t[x];
- return x;
- }
- inline void join1 (int a, int b) {
- a = get (a), b = get (b);
- if (a != b) {
- if (sz[a] > sz[b]) swap (a, b);
- t[a] = b;
- sz[b] += sz[a];
- }
- }
- inline int join (int a, int b) {
- a = get (a), b = get (b);
- if (a != b) {
- if (sz[a] > sz[b]) swap (a, b);
- order.pb ({t[a], a, b, sz[b]});
- t[a] = b;
- sz[b] += sz[a];
- return 1;
- }
- return 0;
- }
- };
- int goo[maxn][maxn], n, m, ans = 0;
- vector<pii> d = {{0, 1}, {1, 0}};
- vector<pii> kek[maxn * maxn * 2];
- pii res = {-1, -1};
- inline int corr (int i, int j) {
- return i >= 0 && j >= 0 && i < n && j < m;
- }
- signed main () {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- cin >> n >> m;
- DSU dsu (n * m);
- for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> goo[i][j];
- for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) for (auto& x: d) {
- int ii = i + x.fi, jj = j + x.se;
- if (corr (ii, jj) && goo[i][j] == goo[ii][jj]) dsu.join1 (i * m + j, ii * m + jj);
- }
- map <pii, int> num;
- int last = 0;
- for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) for (auto& x: d) {
- int ii = i + x.fi, jj = j + x.se;
- if (corr (ii, jj) && goo[i][j] != goo[ii][jj]) {
- pii ptt = {goo[i][j], goo[ii][jj]};
- if (ptt.fi > ptt.se) swap (ptt.fi, ptt.se);
- int x;
- auto it = num.find (ptt);
- if (it == num.end ()) {
- x = last;
- num[ptt] = last++;
- } else {
- x = it->se;
- }
- kek[x].pb ({i * m + j, ii * m + jj});
- }
- }
- for (auto& r: num) {
- int i = r.se;
- int counter = 0;
- for (auto& x: kek[i]) counter += dsu.join (x.fi, x.se);
- for (auto& x: kek[i]) {
- int e = dsu.get (x.fi);
- if (chkmax (ans, sz[e])) res = r.fi;
- }
- while (counter--) dsu.rollback ();
- }
- for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) {
- if (chkmax (ans, sz[i * m + j])) res = {goo[i][j], goo[i][j]};
- }
- cout << ans << '\n' << res.fi << ' ' << res.se << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement