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>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;
const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int N = 510, MAXN=N*N*2;
int n, m, k, u, v, x, y, t, a, b, ans;
char A[N][N];
int id[N][N], cnt;
pii id2[MAXN];
int match[MAXN];
int dist[MAXN];
bool mark[MAXN], VC[MAXN], out[MAXN];
vector<int> G[MAXN];
void addedge(int u, int v){
G[u].pb(v);
G[v].pb(u);
}
bool BFS(){
memset(VC, 0, sizeof(VC));
memset(dist, 31, sizeof(dist));
queue<int> Q;
for (int i=0; i<cnt; i++) if (match[2*i]==-1){
dist[2*i]=0;
Q.push(2*i);
}
bool res=0;
while (Q.size()){
int v=Q.front();
Q.pop();
if (v%2==0){
VC[v]=1;
for (int u:G[v]) if (dist[v]+1<dist[u]){
dist[u]=dist[v]+1;
Q.push(u);
}
continue ;
}
if (match[v]==-1){
res=1;
continue ;
}
dist[match[v]]=dist[v]+1;
Q.push(match[v]);
}
return res;
}
bool dfs(int node){
mark[node]=1;
for (int v:G[node]) if (match[v]==-1 || (dist[node]+1==dist[v] && !mark[match[v]] && dfs(match[v]))){
match[v]=node;
match[node]=v;
return 1;
}
return 0;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin>>n>>m;
for (int i=1; i<=n; i++) for (int j=1; j<=m; j++){
cin>>A[i][j];
if (A[i][j]=='1') id2[id[i][j]=cnt++]={i, j};
else{
int x=i, y=j;
while (A[x][j]=='0') x--;
while (A[i][y]=='0') y--;
addedge(2*id[x][j], 2*id[i][y]+1);
}
}
memset(match, -1, sizeof(match));
while (BFS()){
memset(mark, 0, sizeof(mark));
for (int v=0; v<cnt; v++) if (!mark[2*v] && match[2*v]==-1) dfs(2*v);
}
for (int i=0; i<cnt; i++) if (match[2*i]!=-1){
ans++;
if (VC[2*i]) out[match[2*i]]=1;
else out[2*i]=1;
}
cout<<ans<<'\n';
for (int i=0; i<2*cnt; i++) if (out[i]){
cout<<id2[i>>1].first<<' '<<id2[i>>1].second<<' ';
if (i&1) cout<<"DESNO\n";
else cout<<"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... |