Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define nmax 10005
- using namespace std;
- int N, M, st[nmax], dr[nmax], ans;
- bool viz[nmax];
- vector <int> L[nmax];
- int n, m, a[105][105], b[105][105];
- bool Cupleaza(int k)
- {
- if(viz[k]) return false;
- viz[k] = true;
- for(auto i : L[k])
- {
- if(!dr[i])
- {
- st[k] = i;
- dr[i] = k;
- return true;
- }
- }
- for(auto i : L[k])
- if(Cupleaza(dr[i]))
- {
- st[k] = i;
- dr[i] = k;
- return true;
- }
- return false;
- }
- void Read()
- {
- cin >> n >> m;
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= m; j++)
- {
- cin >> a[i][j];
- if ((i + j) % 2 == 0) b[i][j] = ++N;
- else b[i][j] = ++M;
- }
- }
- void Graf()
- {
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= m; j++)
- if ((i + j) % 2 == 0 && a[i][j] == 1)
- {
- if (a[i-1][j] == 1) L[ b[i][j] ].push_back(b[i-1][j]);
- if (a[i+1][j] == 1) L[ b[i][j] ].push_back(b[i+1][j]);
- if (a[i][j+1] == 1) L[ b[i][j] ].push_back(b[i][j+1]);
- if (a[i][j-1] == 1) L[ b[i][j] ].push_back(b[i][j-1]);
- }
- }
- void Rezolva()
- {
- int i, gata = 0;
- while(!gata)
- {
- gata = 1;
- for(i = 1; i <= N; i++) viz[i] = 0;
- for(i = 1; i <= N; i++)
- if(st[i] == 0 && Cupleaza(i))
- {
- ++ans;
- gata = 0;
- }
- }
- cout << ans;
- }
- int main()
- {
- Read();
- Graf();
- Rezolva();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement