Advertisement
1nikitas

Untitled

Nov 3rd, 2021
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. int n;
  9. vector<pair<int, int>> G;
  10. vector<int> f[5000], g[5000], c[5000], rev[5000];
  11. bool used[5000];
  12.  
  13. int dfs (int v, int C, int d) {
  14. used[v] = true;
  15. if (v == n - 1)
  16. return C;
  17. for (int i = 0; i < g[v].size(); i++)
  18. if ( !used[g[v][i]] && c[v][i] - f[v][i] >= d) {
  19. int k = dfs (g[v][i], min(C, c[v][i] - f[v][i]), d);
  20. if (k) {
  21. f[v][i] += k;
  22. f[g[v][i]][rev[v][i]] -= k;
  23. return k;
  24. }
  25. }
  26. return 0;
  27. }
  28. int valents (char el) {
  29. switch (el) {
  30. case 'H':
  31. return 1;
  32. case 'O':
  33. return 2;
  34. case 'N':
  35. return 3;
  36. case 'C':
  37. return 4;
  38. }
  39. return 0;
  40. }
  41.  
  42. void add_edge (int u, int v, int w) {
  43. G.emplace_back(u, v);
  44. f[u].push_back(0);
  45. c[u].push_back(w);
  46. g[u].push_back(v);
  47. rev[u].push_back(g[v].size());
  48. f[v].push_back(0);
  49. c[v].push_back(0);
  50. g[v].push_back(u);
  51. rev[v].push_back(g[u].size() - 1);
  52. }
  53.  
  54. bool checkEquals(string st[8888], int h, int w){
  55. bool bw = true;
  56. long long valB = 0;
  57. long long valW = 0;
  58. for (int i{0}; i < h; i++) {
  59. for (int j{0}; j < w; j++){
  60. if(bw)
  61. valB += valents(st[i][j]);
  62. else
  63. valW += valents(st[i][j]);
  64. bw = !bw;
  65. }
  66. }
  67. return valB == valW;
  68. }
  69. bool cheack(int val, int h, int w, int i , int j, string st[8888]){
  70. for (int k{i-1}; k < i + 2; k++) {
  71. for (int m{j-1}; m < j + 2; m++){
  72. if(k > -1 && k < h && m > -1 && m < w && !(m==j && k==i)){
  73. if(st[k][m]!='.'){
  74. val--;
  75. }
  76. }
  77. }
  78. }
  79. return val > 1;
  80. }
  81. bool checkNeib(string st[8888], int h, int w){
  82. int val;
  83. for (int i{0}; i < h; i++) {
  84. for (int j{0}; j < w; j++){
  85. val = valents(st[i][j]);
  86. if(cheack(val, h, w, i, j, st)){
  87. return false;
  88. }
  89. }
  90. }
  91. return true;
  92. }
  93. int main () {
  94. int h, w;
  95. cin >> h >> w;
  96. n = 1;
  97. string st[8888];
  98. bool flag = true;
  99. int num[51][51];
  100. for (int i{0}; i < h; i++) {
  101. cin >> st[i];
  102. for (int j = 0; j < w; j++) {
  103. num[i][j] = i * w + j + 1;
  104. if (st[i][j] != '.')
  105. flag = false;
  106. }
  107. }
  108. if(h ==1 || w == 1){
  109. cout << "Valid";
  110. return 0;
  111. }
  112. if (flag) {
  113. cout << "Invalid";
  114. return 0;
  115. }
  116. long long potok = 0;
  117. n = h*w + 2;
  118. for (int i{0}; i < h; i++)
  119. for (int j{0}; j < w; j++) {
  120. if ( ((i & 1) && (j & 1)) || (!(i&1) && !(j&1)) ) {
  121. add_edge (0, num[i][j], valents(st[i][j]));
  122. potok += valents(st[i][j]);
  123. if (i + 1 < h)
  124. add_edge (num[i][j], num[i+1][j], 1);
  125. if (j + 1 < w)
  126. add_edge (num[i][j], num[i][j+1], 1);
  127. if (i - 1 >= 0)
  128. add_edge (num[i][j], num[i-1][j], 1);
  129. if (j - 1 >= 0)
  130. add_edge (num[i][j], num[i][j-1], 1);
  131. } else
  132. add_edge (num[i][j], n - 1, valents(st[i][j]));
  133. }
  134.  
  135. long long s = 0;
  136. for (int i = (1 << 30); i >= 1; i >>= 1) {
  137. while (true) {
  138. memset(used, false, sizeof used);
  139. int k = dfs(0, 1 << 30, i);
  140. if (!k)
  141. break;
  142. s += k;
  143. }
  144. }
  145. if(!checkEquals(st, h, w)){
  146. cout << "Invalid";
  147. return 0;
  148. }
  149. cout << ((s == potok) ? "Valid" : "Invalid");
  150. return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement