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 all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define PB push_back
#define pii pair<int,int>
#define MP make_pair
#define ft first
#define sd second
using namespace std;
const int N = 510;
const int K = 1520;
const int oo = 2e9;
const int md = int(1e9) + 7;
vector<pii> vc[2];
vector<vector<int> > g;
vector<bool> used, was;
vector<int> mt, nm;
char c[N][N];
int n, m, mrk[N][N][2], lf[N][N];
bool try_kuhn(int v){
if (used[v]) return 0;
used[v] = 1;
for (int u : g[v]){
if (mt[u] == -1 || try_kuhn(mt[u])){
mt[u] = v;
return 1;
}
}
return 0;
}
mt19937 rnd(chrono::system_clock().now().time_since_epoch().count());
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("in.txt","r",stdin);
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> c[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++){
if (j == 0){
lf[i][j] = -1;
if (c[i][j] == '1')
lf[i][j] = j;
} else {
if (c[i][j] == '1')
lf[i][j] = j;
else lf[i][j] = lf[i][j - 1];
}
if (c[i][j] == '0') continue;
if (i < n - 1 && c[i + 1][j] == '0') {
mrk[i][j][0] = sz(vc[0]);
vc[0].PB(MP(i, j));
}
if (j < m - 1 && c[i][j + 1] == '0') {
mrk[i][j][1] = sz(vc[1]);
vc[1].PB(MP(i, j));
}
}
g.clear();
for (int it = 0; it < sz(vc[0]); it++){
g.resize(sz(g) + 1);
g.back().clear();
int x = vc[0][it].ft;
int y = vc[0][it].sd;
int xx = x + 1;
while (xx < n && c[xx][y] == '0'){
int yy = lf[xx][y];
g.back().PB(mrk[xx][yy][1]);
xx++;
}
}
used.resize(sz(vc[0]));
nm.resize(sz(vc[0]));
was.resize(sz(vc[0]));
mt.resize(sz(vc[1]));
fill(all(mt), -1);
fill(all(was), 0);
for (int i = 0; i < sz(vc[0]); i++)
nm[i] = i;
shuffle(all(nm), rnd);
for (int it = 0; it < sz(vc[0]); it++) {
int i = nm[it];
fill(all(used), 0);
was[i] = try_kuhn(i);
}
int ans = 0;
for (int i = 0; i < sz(vc[0]); i++)
ans += was[i];
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |