이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <deque>
#include <map>
#include <set>
#include <complex>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <random>
#define ft first
#define sc second
#define pb push_back
#define len(v) (int)v.size()
// #define int ll
using namespace std;
typedef long long ll;
vector<int> type;
vector<bool> used;
vector<vector<int>> g;
int cnt1 = 0, cnt2 = 0;
void dfs(int v) {
used[v] = 1;
if(type[v] == 1)
cnt1++;
else
cnt2++;
for (auto x : g[v]) {
if(used[x])
continue;
dfs(x);
}
}
signed main() {
#ifdef PC
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, cnt = 0, ans = 0;
cin >> n >> m;
vector<vector<char>> mat(n, vector<char> (m));
vector<vector<pair<int, int>>> have(n, vector<pair<int, int>> (m, {-1, -1}));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> mat[i][j];
}
}
for (int i = 0; i < n - 2; i++) {
for (int j = 0; j < m; j++) {
if(!(mat[i][j] == 'R' && mat[i + 1][j] == 'G' && mat[i + 2][j] == 'W'))
continue;
type.pb(1);
have[i][j].ft = cnt, have[i + 1][j].ft = cnt, have[i + 2][j].ft = cnt;
cnt++;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m - 2; j++) {
if(!(mat[i][j] == 'R' && mat[i][j + 1] == 'G' && mat[i][j + 2] == 'W'))
continue;
type.pb(2);
have[i][j].sc = cnt, have[i][j + 1].sc = cnt, have[i][j + 2].sc = cnt;
cnt++;
}
}
g = vector<vector<int>> (cnt);
used = vector<bool> (cnt, 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(have[i][j].ft == -1 || have[i][j].sc == -1)
continue;
g[have[i][j].ft].pb(have[i][j].sc);
g[have[i][j].sc].pb(have[i][j].ft);
}
}
for (int i = 0; i < cnt; i++) {
if(used[i])
continue;
cnt1 = 0, cnt2 = 0;
dfs(i);
ans += max(cnt1, cnt2);
}
cout << ans << 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... |