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>
using namespace std;
typedef long long ll;
const int N = 5e2+4;
const int inf = 1e9;
int n,m,n1,n2,ans;
vector<int> adj[N*N];
int L[N][N],U[N][N];
int _u[N*N],_v[N*N],dis[N*N];
pair<int,int> posU[N*N],posL[N*N];
char lab[N][N];
queue<int> d;
bool vis[N*N];
bool bfs(){
for (int i=1; i<=n1; i++){
if (!_u[i]){ dis[i] = 0; d.push(i);}
else dis[i] = inf;
}
dis[0] = inf;
for (int u; d.size(); d.pop()){ u = d.front();
for (int v : adj[u]) if (dis[_v[v]] == inf){
dis[_v[v]] = dis[u] + 1; d.push(_v[v]);
}
}
return dis[0] != inf;
}
bool dfs1(int u){
if (!u) return 1;
for (int v : adj[u]) if (dis[_v[v]] == dis[u]+1 && dfs1(_v[v])){
_v[v] = u, _u[u] = v;
return 1;
}
dis[u] = inf;
return 0;
}
void dfs2(int u){ vis[u] = 1;
for (int v : adj[u]) if (!vis[_v[v]]) dfs2(_v[v]);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i=0; i<n; i++) for (int j=0; j<m; j++){ cin >> lab[i][j];
if (lab[i][j] == '0'){
if (lab[i-1][j] == '0') U[i][j] = U[i-1][j];
else U[i][j] = ++n1, posU[n1] = {i, j};
if (lab[i][j-1] == '0') L[i][j] = L[i][j-1];
else L[i][j] = ++n2, posL[n2] = {i, j};
adj[U[i][j]].push_back(L[i][j]);
}
}
while (bfs()) for (int i=1; i<=n1; i++)
if (!_u[i] && dfs1(i)) ans++;
cout << ans << "\n";
for (int u=1; u<=n1; u++) if (!_u[u]) dfs2(u);
for (int u=1; u<=n1; u++) if (_u[u]){
if (vis[u])
cout << posL[_u[u]].first+1 << " " << posL[_u[u]].second << " DESNO\n";
else
cout << posU[u].first << " " << posU[u].second+1 << " DOLJE\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... |