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("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 500005
#define MOD ll(998244353)
using namespace std;
typedef long long ll;
typedef long double ld;
int mt[N], idr[505][505][2];
pair <int, int> conv[N][2];
bool lft[N];
vector <int> g[N];
bool mk[N], mkr[N], was[N];
set <pair <int, int> > se;
bool kuna(int v)
{
if (mk[v]) return 0;
mk[v] = 1;
for (auto it : g[v])
{
if (mt[it] == -1 || kuna(mt[it]))
{
mt[it] = v;
was[v] = 1;
return 1;
}
}
return 0;
}
void dfs(int v, int tp)
{
mkr[v] = 1;
if (!tp)
{
for (auto it : g[v])
if (!mkr[it] && se.find({v, it}) == se.end()) dfs(it, 1);
}
else if (mt[v] != -1 && !mkr[mt[v]]) dfs(mt[v], 0);
}
int main()
{
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
string s[n];
for (int i = 0; i < n; i++) cin >> s[i];
int id = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {conv[id][0] = {i, j}; idr[i][j][0] = id++; lft[id] = 1; conv[id][1] = {i, j}; idr[i][j][1] = id++;}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (s[i][j] == '0')
{
int x = i;
while (s[x][j] == '0') x--;
int y = j;
while (s[i][y] == '0') y--;
g[idr[x][j][1]].pb(idr[i][y][0]);
}
memset(mt, -1, sizeof(mt));
for (int i = 0; i < id; i++)
{
if (sz(g[i]) == 0 || was[i]) continue;
memset(mk, 0, sizeof(mk));
was[i] = kuna(i);
}
vector <pair <pair <int, int>, int> > ans; ans.clear();
for (int i = 0; i < id; i++) if (mt[i] != -1) se.insert({mt[i], i});
for (int i = 0; i < id; i++) if (lft[i] && !was[i] && !mkr[i])
dfs(i, 0);
for (int i = 0; i < id; i++)
{
if (lft[i] && !mkr[i]) ans.pb({conv[i][1], 1});
if (mkr[i] && !lft[i]) ans.pb({conv[i][0], 0});
}
cout << sz(ans) << endl;
for (auto it : ans) cout << it.F.F + 1 << " " << it.F.S + 1 << " " << (it.S == 0 ? "DESNO" : "DOLJE") << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |