This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ST first
#define ND second
#define PB push_back
using namespace std;
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
const int nax = 510;
int n, m;
char t[nax][nax];
int lft[nax][nax], up[nax][nax];
vi V[nax * nax * 2];
bool vis[nax * nax * 2];
int match[nax * nax * 2];
bool aug(int x) {
vis[x] = true;
for(int nbh : V[x]) if(match[nbh] == -1) {
match[x] = nbh;
match[nbh] = x;
return true;
}
for(int nbh : V[x]) if(!vis[nbh]) {
if(aug(match[nbh])) {
match[x] = nbh;
match[nbh] = x;
return true;
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
cin >> t[i][j];
if(t[i][j] == '1') {
lft[i][j] = i * m + j;
up[i][j] = i * m + j;
} else {
lft[i][j] = lft[i][j - 1];
up[i][j] = up[i - 1][j];
V[up[i][j]].PB(lft[i][j] + n * m);
V[lft[i][j] + n * m].PB(up[i][j]);
}
}
}
for(int i = 0; i < n * m * 2; ++i) match[i] = -1;
while(true) {
for(int i = 0; i < n * m * 2; ++i) vis[i] = false;
bool any = false;
for(int i = 0; i < n * m * 2; ++i) {
if(!vis[i] && match[i] == -1 && aug(i)) any = true;
}
if(any) break;
}
int ans = 0;
for(int i = 0; i < n * m; ++i) {
ans += match[i] != -1;
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |