Advertisement
a53

CMGB

a53
Apr 10th, 2022
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define nmax 10005
  3. using namespace std;
  4.  
  5. int N, M, st[nmax], dr[nmax], ans;
  6. bool viz[nmax];
  7. vector <int> L[nmax];
  8. int n, m, a[105][105], b[105][105];
  9.  
  10. bool Cupleaza(int k)
  11. {
  12. if(viz[k]) return false;
  13. viz[k] = true;
  14. for(auto i : L[k])
  15. {
  16. if(!dr[i])
  17. {
  18. st[k] = i;
  19. dr[i] = k;
  20. return true;
  21. }
  22. }
  23. for(auto i : L[k])
  24. if(Cupleaza(dr[i]))
  25. {
  26. st[k] = i;
  27. dr[i] = k;
  28. return true;
  29. }
  30. return false;
  31. }
  32.  
  33. void Read()
  34. {
  35. cin >> n >> m;
  36. for (int i = 1; i <= n; i++)
  37. for (int j = 1; j <= m; j++)
  38. {
  39. cin >> a[i][j];
  40. if ((i + j) % 2 == 0) b[i][j] = ++N;
  41. else b[i][j] = ++M;
  42. }
  43. }
  44.  
  45. void Graf()
  46. {
  47. for (int i = 1; i <= n; i++)
  48. for (int j = 1; j <= m; j++)
  49. if ((i + j) % 2 == 0 && a[i][j] == 1)
  50. {
  51. if (a[i-1][j] == 1) L[ b[i][j] ].push_back(b[i-1][j]);
  52. if (a[i+1][j] == 1) L[ b[i][j] ].push_back(b[i+1][j]);
  53. if (a[i][j+1] == 1) L[ b[i][j] ].push_back(b[i][j+1]);
  54. if (a[i][j-1] == 1) L[ b[i][j] ].push_back(b[i][j-1]);
  55. }
  56. }
  57.  
  58. void Rezolva()
  59. {
  60. int i, gata = 0;
  61. while(!gata)
  62. {
  63. gata = 1;
  64. for(i = 1; i <= N; i++) viz[i] = 0;
  65. for(i = 1; i <= N; i++)
  66. if(st[i] == 0 && Cupleaza(i))
  67. {
  68. ++ans;
  69. gata = 0;
  70. }
  71. }
  72. cout << ans;
  73. }
  74.  
  75. int main()
  76. {
  77. Read();
  78. Graf();
  79. Rezolva();
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement