#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <random>
using namespace std;
const int N = 3001;
int a[N][N];
int read() {
char c;
cin >> c;
if (c == 'R') return 0;
if (c == 'G') return 1;
if (c == 'W') return 2;
assert(false);
return -1;
}
int n, m;
int ok(int i, int j, int dir) {
if (dir == 0) {
if (i == 0 || i + 1 == n) return 0;
return a[i - 1][j] == 0 && a[i][j] == 1 && a[i + 1][j] == 2;
} else {
if (j == 0 || j + 1 == m) return 0;
return a[i][j - 1] == 0 && a[i][j] == 1 && a[i][j + 1] == 2;
}
}
vector<pair<int, int>> gt(int i, int j, int dir) {
if (dir == 0) {
return {{i - 1, j}, {i, j}, {i + 1, j}};
} else {
return {{i, j - 1}, {i, j}, {i, j + 1}};
}
}
bool inter(vector<pair<int, int>> &x, vector<pair<int, int>> &y) {
for (auto f : x) {
for (auto s : y) {
if (f == s) return 1;
}
}
return 0;
}
vector<vector<int>> g;
vector<int> used;
vector<int> clr;
void dfs(int v, int c) {
clr[v] = c;
used[v] = 1;
for (auto t : g[v]) {
if (!used[t]) dfs(t, c ^ 1);
}
}
pair<int, int> parse(int v) {
v %= n * m;
return {v / m, v % m};
}
vector<int> mt;
vector<vector<int>> ng;
int sm(int v) {
if (used[v]) return 0;
used[v] = 1;
for (auto t : ng[v]) {
if (mt[t] == -1 || sm(mt[t])) {
mt[t] = v;
return 1;
}
}
return 0;
}
int solve_mathing() {
int f = (int)g.size();
vector<int> lp, rp;
for (int i = 0; i < f; i++) {
if (!clr[i]) lp.push_back(i);
else rp.push_back(i);
}
mt.clear();
ng.clear();
mt.resize(rp.size(), -1);
ng.resize(lp.size());
for (int i = 0; i < f; i++) {
for (auto j : g[i]) {
if (!clr[i]) {
int x = lower_bound(lp.begin(), lp.end(), i) - lp.begin();
int y = lower_bound(rp.begin(), rp.end(), j) - rp.begin();
ng[x].push_back(y);
}
}
}
used.clear();
used.resize(lp.size());
for (int i = 0; i < (int)lp.size(); i++) {
fill(used.begin(), used.end(), 0);
sm(i);
}
int ans = 0;
for (auto t : mt) ans += (t != -1);
return ans;
}
int solve() {
vector<int> tk;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (ok(i, j, 0)) tk.push_back({i * m + j});
if (ok(i, j, 1)) tk.push_back({i * m + j + n * m});
}
}
auto get = [&](int x) {
int v = tk[x];
int tp = (v / (n * m));
v %= n * m;
int i = v / m;
int j = v % m;
return gt(i, j, tp);
};
g.clear();
used.clear();
clr.clear();
g.resize(tk.size());
used.resize(tk.size());
clr.resize(tk.size());
for (int i = 0; i < (int)tk.size(); i++) {
for (int j = i + 1; j < (int)tk.size(); j++) {
vector<pair<int, int>> f = get(i);
vector<pair<int, int>> s = get(j);
if (inter(f, s)) {
g[i].push_back(j);
g[j].push_back(i);
auto x = parse(tk[i]);
auto y = parse(tk[j]);
int lol = abs(x.first - y.first) + abs(x.second - y.second);
assert(lol == 0 || lol == 2);
}
}
}
int f = (int)(tk.size());
{
for (int i = 0; i < f; i++) {
if (!used[i]) dfs(i, 0);
}
for (int i = 0; i < f; i++) {
for (auto j : g[i]) {
assert(clr[i] != clr[j]);
}
}
}
return solve_mathing();
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = read();
}
}
cout << solve() << endl;
// while (true) {
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// a[i][j] = rand() % 3;
// }
// }
// string lol = "RGW";
// cout << "TEST" << endl;
// cout << n << ' ' << m << endl;
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// cout << lol[a[i][j]];
// }
// cout << endl;
// }
// cout << solve() << endl;
// }
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
380 KB |
Output is correct |
5 |
Correct |
4 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
380 KB |
Output is correct |
5 |
Correct |
4 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
380 KB |
Output is correct |
5 |
Correct |
4 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |