Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define endl "\n"
- using namespace std;
- using ll = long long;
- using ld = long double;
- using pii = pair<int, int>;
- constexpr int N = 1e3 + 5;
- int n, m, field[N][N], di[] = {0, 1, -1, 0}, dj[] = {1, 0, 0, -1}, ans = 0, temp;
- pii ansp, id;
- map<pii, vector<set<int>>> mp;
- bool used[N * N];
- struct Dsu{
- vector<int> p, s;
- Dsu(int n){
- s = vector<int>(n * n, 1);
- p.resize(n * n);
- iota(p.begin(), p.end(), 0);
- }
- int get(int i){
- return i == p[i] ? i : p[i] = get(p[i]);
- }
- int get(int i, int j){
- return get(i * N + j);
- }
- void uni(int a, int b){
- a = get(a);
- b = get(b);
- if (a != b){
- if (s[a] < s[b])
- swap(a, b);
- s[a] += s[b];
- p[b] = a;
- }
- }
- } d(N);
- bool check(int i, int j, int x, int y){
- return i >= 0 && j >= 0 && i < n && j < m && field[i][j] == field[x][y];
- }
- bool check2(int i, int j, int x, int y){
- return i >= 0 && j >= 0 && i < n && j < m && field[i][j] != field[x][y];
- }
- void make_ed(pii id, int i, int j){
- if (mp[id].size() < min(i, j))
- mp[id].resize(min(i, j));
- mp[id][i].insert(j);
- }
- int dfs(pii id, int i){
- used[i] = 1;
- int val = d.s[i];
- for (int j: mp[id][i])
- if (!used[j])
- val += dfs(id, j);
- return val;
- }
- void Solve(){
- cin >> n >> m;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- cin >> field[i][j];
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- for (int k = 0; k < 4; k++)
- if (check(i + di[k], j + dj[k], i, j))
- d.uni(d.get(i, j), d.get(i + di[k], j + dj[k]));
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- for (int k = 0; k < 4; k++)
- if (check2(i + di[k], j + dj[k], i, j)){
- id = {min(field[i][j], field[i + di[k]][j + dj[k]]),
- max(field[i][j], field[i + di[k]][j + dj[k]])};
- make_ed(id, d.get(i, j), d.get(i + di[k], j + dj[k]));
- }
- for (auto& [k, m]: mp){
- memset(used, 0, sizeof used);
- for (int i = 0; i < m.size(); i++){
- if (!used[i]){
- temp = dfs(id, i);
- if (ans < temp){
- ans = temp;
- ansp = k;
- }
- }
- }
- }
- if (ans == 0){
- cout << n * m << endl << field[0][0] << " " << field[0][0];
- return;
- }
- cout << ans << endl << ansp.first << " " << ansp.second;
- }
- int main(){
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- Solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement