이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 10;
int val[MAXN][MAXN]; // 0 red, 1 g, 2 w
int n, m;
int uf[2*MAXN*MAXN]; // cuz im lazy
int size[2*MAXN*MAXN];
int common[MAXN*MAXN];
vector<int> edges[2*MAXN*MAXN];
bool color[2*MAXN*MAXN];
bool visited[2*MAXN*MAXN];
int find(int i) {
if (uf[i] == -1) return i;
return (uf[i] = find(uf[i]));
}
void merge(int a, int b) {
int ida = find(a);
int idb = find(b);
if (ida == idb) return;
edges[a].push_back(b);
edges[b].push_back(a);
size[ida] += size[idb];
uf[idb] = ida;
}
bool is_vertical(int i, int j) {
if (i >= n-2) return 0;
return (val[i][j] == 0 && val[i+1][j] == 1 && val[i+2][j] == 2);
}
bool is_horizontal(int i, int j) {
if (j >= m-2) return 0;
return (val[i][j] == 0 && val[i][j+1] == 1 && val[i][j+2] == 2);
}
int get(int i) {
vector<int> q;
q.push_back(i);
visited[i] = 1;
int num_0 = 1;
int num_1 = 0;
while (!q.empty()) {
int cur = q.back();
q.pop_back();
for (int next: edges[cur]) {
if (visited[next]) continue;
visited[next] = 1;
color[next] = 1-color[cur];
if (color[next]) num_1++;
else num_0++;
q.push_back(next);
}
}
return max(num_0, num_1);
}
void add(int a, int b) {
if (common[a] == -1) common[a] = b;
else merge(common[a], b);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) common[i*m+j] = -1;
}
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < m; j++) {
if (s[j] == 'R') val[i][j] = 0;
if (s[j] == 'G') val[i][j] = 1;
if (s[j] == 'W') val[i][j] = 2;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int chash = i*m+j;
if (is_vertical(i, j)) {
size[chash] = 1;
uf[chash] = -1;
add(chash, chash);
add(chash+m, chash);
add(chash+2*m, chash);
}
if (is_horizontal(i, j)) {
size[chash+n*m] = 1;
uf[chash+n*m] = -1;
add(chash, chash+n*m);
add(chash+1, chash+n*m);
add(chash+2, chash+n*m);
}
}
}
int answ = 0;
for (int i = 0; i < 2*n*m; i++) {
if (uf[i] == -1) answ += get(i);
}
cout << answ << "\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... |