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, gg;
vector<bool> used, was;
vector<int> mt, mkt;
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;
}
void dfs(int v, int tp){
if (tp){
if (mkt[v] >= 0) return;
mkt[v] = 0;
if (mt[v] >= 0)
dfs(mt[v], 0);
} else {
if (used[v]) return;
used[v] = 1;
for (int u : g[v])
if (mt[u] != v)
dfs(u, 1);
}
}
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();
gg.clear();
gg.resize(sz(vc[1]));
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]);
gg[mrk[xx][yy][1]].PB(it);
xx++;
}
}
used.resize(sz(vc[0]));
was.resize(sz(vc[0]));
mt.resize(sz(vc[1]));
mkt.resize(sz(vc[1]));
fill(all(mt), -1);
fill(all(was), 0);
for (int i = 0; i < sz(vc[0]); i++) {
if (was[i]) continue;
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 << '\n';
fill(all(mkt), -1);
fill(all(used), 0);
for (int i = 0; i < sz(vc[0]); i++)
if (!was[i])
dfs(i, 0);
for (int i = 0; i < sz(vc[0]); i++)
if (!used[i]){
cout << vc[0][i].ft + 1 << " " << vc[0][i].sd + 1 << " DOLJE\n";
}
for (int i = 0; i < sz(vc[1]); i++)
if (mkt[i] >= 0){
cout << vc[1][i].ft + 1 << " " << vc[1][i].sd + 1 << " DESNO\n";
}
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... |