Submission #814546

#TimeUsernameProblemLanguageResultExecution timeMemory
814546SteGGDango Maker (JOI18_dango_maker)C++17
13 / 100
124 ms235432 KiB
#include <bits/stdc++.h> #define int long long #define VERTICAL 1 #define HORIZONTAL 2 #define NOT_DONE 1 #define DONE 2 using namespace std; void open(){ if(fopen("input.inp", "r")){ freopen("input.inp", "r", stdin); //freopen("output.out", "w", stdout); } } const int maxn = 1e7; int n, m; char board[3001][3001]; vector<int> G[maxn]; int vertical_change[3001][3001], horizontal_change[3001][3001]; pair<int, int> vertical_parent[3001][3001], horizontal_parent[3001][3001]; bool vertical_check[3001][3001], horizontal_check[3001][3001]; int cnt; int vertical_state[3001][3001], horizontal_state[3001][3001]; bool check[maxn]; int bfs(int root){ queue<pair<int, bool>> q; q.push({root, true}); int total = 0; int cur = 0; while(!q.empty()){ pair<int, int> first = q.front(); q.pop(); int u = first.first; if(check[u]) continue; check[u] = true; total++; cur += first.second; for(int v : G[u]){ q.push({v, first.second ^ 1}); } } return max(cur, total - cur); } void build(int x, int y, int direction){ if(direction == VERTICAL){ if(vertical_state[x][y]) return; vertical_state[x][y] = NOT_DONE; int cur_index = vertical_change[x][y]; int new_index; if(x + 2 > n || board[x + 1][y] != 'G' || board[x + 2][y] != 'W') return; // To R: if(horizontal_check[x][y]){ pair<int, int> temp = make_pair(x, y); new_index = horizontal_change[x][y]; if(horizontal_state[temp.first][temp.second] != NOT_DONE){ G[cur_index].push_back(new_index); G[new_index].push_back(cur_index); build(temp.first, temp.second, HORIZONTAL); } } // To G: if(horizontal_parent[x + 1][y] != make_pair(0ll, 0ll)){ pair<int, int> temp = horizontal_parent[x + 1][y]; new_index = horizontal_change[temp.first][temp.second]; if(horizontal_state[temp.first][temp.second] != NOT_DONE){ G[cur_index].push_back(new_index); G[new_index].push_back(cur_index); build(temp.first, temp.second, HORIZONTAL); } } // To W: if(horizontal_parent[x + 2][y] != make_pair(0ll, 0ll)){ pair<int, int> temp = horizontal_parent[x + 2][y]; new_index = horizontal_change[temp.first][temp.second]; if(horizontal_state[temp.first][temp.second] != NOT_DONE){ G[cur_index].push_back(new_index); G[new_index].push_back(cur_index); build(temp.first, temp.second, HORIZONTAL); } } vertical_state[x][y] = DONE; }else{ horizontal_state[x][y] = NOT_DONE; int cur_index = horizontal_change[x][y]; int new_index; if(y + 2 > m || board[x][y + 1] != 'G' || board[x][y + 2] != 'W') return; // To R: if(vertical_check[x][y]){ pair<int, int> temp = make_pair(x, y); new_index = vertical_change[x][y]; if(vertical_state[temp.first][temp.second] != NOT_DONE){ G[cur_index].push_back(new_index); G[new_index].push_back(cur_index); build(temp.first, temp.second, VERTICAL); } } // To G: if(vertical_parent[x][y + 1] != make_pair(0ll, 0ll)){ pair<int, int> temp = vertical_parent[x][y + 1]; new_index = vertical_change[temp.first][temp.second]; if(vertical_state[temp.first][temp.second] != NOT_DONE){ G[cur_index].push_back(new_index); G[new_index].push_back(cur_index); build(temp.first, temp.second, VERTICAL); } } // To W: if(vertical_parent[x][y + 1] != make_pair(0ll, 0ll)){ pair<int, int> temp = vertical_parent[x][y + 1]; new_index = vertical_change[temp.first][temp.second]; if(vertical_state[temp.first][temp.second] != NOT_DONE){ G[cur_index].push_back(new_index); G[new_index].push_back(cur_index); build(temp.first, temp.second, VERTICAL); } } horizontal_state[x][y] = DONE; } } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); open(); cin >> n >> m; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ cin >> board[i][j]; } } cnt = 0; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(board[i][j] == 'R'){ // horizontal: if(j + 2 <= m && board[i][j + 1] == 'G' && board[i][j + 2] == 'W'){ horizontal_parent[i][j] = make_pair(i, j); horizontal_change[i][j] = ++cnt; horizontal_parent[i][j + 1] = horizontal_parent[i][j + 2] = horizontal_parent[i][j]; horizontal_check[i][j] = true; } // vertical: if(i + 2 <= n && board[i + 1][j] == 'G' && board[i + 2][j] == 'W'){ vertical_parent[i][j] = make_pair(i, j); vertical_change[i][j] = ++cnt; vertical_parent[i + 1][j] = vertical_parent[i + 2][j] = vertical_parent[i][j]; vertical_check[i][j] = true; } } } } if(cnt == 0){ cout << 0 << endl; return 0; } for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(board[i][j] == 'R'){ if(vertical_check[i][j]){ build(i, j, VERTICAL); }else{ build(i, j, HORIZONTAL); } } } } int result = 0; for(int i = 1; i <= cnt; i++){ if(!check[i]){ result += bfs(i); } } cout << result << endl; return 0; }

Compilation message (stderr)

dango_maker.cpp: In function 'void open()':
dango_maker.cpp:12:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |   freopen("input.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...