Advertisement
limimage

TASK G5

Sep 23rd, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define endl "\n"
  4. using namespace std;
  5. using ll = long long;
  6. using ld = long double;
  7. using pii = pair<int, int>;
  8.  
  9. constexpr int N = 1e3 + 5;
  10.  
  11. int n, m, field[N][N], di[] = {0, 1, -1, 0}, dj[] = {1, 0, 0, -1}, ans = 0, temp;
  12. pii ansp, id;
  13. map<pii, vector<set<int>>> mp;
  14. bool used[N * N];
  15.  
  16. struct Dsu{
  17. vector<int> p, s;
  18. Dsu(int n){
  19. s = vector<int>(n * n, 1);
  20. p.resize(n * n);
  21. iota(p.begin(), p.end(), 0);
  22. }
  23. int get(int i){
  24. return i == p[i] ? i : p[i] = get(p[i]);
  25. }
  26. int get(int i, int j){
  27. return get(i * N + j);
  28. }
  29. void uni(int a, int b){
  30. a = get(a);
  31. b = get(b);
  32. if (a != b){
  33. if (s[a] < s[b])
  34. swap(a, b);
  35. s[a] += s[b];
  36. p[b] = a;
  37. }
  38. }
  39. } d(N);
  40.  
  41.  
  42.  
  43. bool check(int i, int j, int x, int y){
  44. return i >= 0 && j >= 0 && i < n && j < m && field[i][j] == field[x][y];
  45. }
  46.  
  47. bool check2(int i, int j, int x, int y){
  48. return i >= 0 && j >= 0 && i < n && j < m && field[i][j] != field[x][y];
  49. }
  50.  
  51. void make_ed(pii id, int i, int j){
  52. if (mp[id].size() < min(i, j))
  53. mp[id].resize(min(i, j));
  54. mp[id][i].insert(j);
  55. }
  56.  
  57. int dfs(pii id, int i){
  58. used[i] = 1;
  59. int val = d.s[i];
  60. for (int j: mp[id][i])
  61. if (!used[j])
  62. val += dfs(id, j);
  63. return val;
  64. }
  65.  
  66. void Solve(){
  67. cin >> n >> m;
  68. for (int i = 0; i < n; i++)
  69. for (int j = 0; j < m; j++)
  70. cin >> field[i][j];
  71.  
  72. for (int i = 0; i < n; i++)
  73. for (int j = 0; j < m; j++)
  74. for (int k = 0; k < 4; k++)
  75. if (check(i + di[k], j + dj[k], i, j))
  76. d.uni(d.get(i, j), d.get(i + di[k], j + dj[k]));
  77.  
  78. for (int i = 0; i < n; i++)
  79. for (int j = 0; j < m; j++)
  80. for (int k = 0; k < 4; k++)
  81. if (check2(i + di[k], j + dj[k], i, j)){
  82. id = {min(field[i][j], field[i + di[k]][j + dj[k]]),
  83. max(field[i][j], field[i + di[k]][j + dj[k]])};
  84. make_ed(id, d.get(i, j), d.get(i + di[k], j + dj[k]));
  85. }
  86.  
  87. for (auto& [k, m]: mp){
  88. memset(used, 0, sizeof used);
  89. for (int i = 0; i < m.size(); i++){
  90. if (!used[i]){
  91. temp = dfs(id, i);
  92. if (ans < temp){
  93. ans = temp;
  94. ansp = k;
  95. }
  96. }
  97.  
  98. }
  99. }
  100.  
  101. if (ans == 0){
  102. cout << n * m << endl << field[0][0] << " " << field[0][0];
  103. return;
  104. }
  105. cout << ans << endl << ansp.first << " " << ansp.second;
  106. }
  107.  
  108. int main(){
  109. ios::sync_with_stdio(false);
  110. cin.tie(nullptr);
  111. cout.tie(nullptr);
  112. Solve();
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement