Submission #814405

# Submission time Handle Problem Language Result Execution time Memory
814405 2023-08-08T07:18:56 Z SteGG Dango Maker (JOI18_dango_maker) C++17
0 / 100
116 ms 235200 KB
#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 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(state[x][y]) return;
	state[x][y] = NOT_DONE;

	if(direction == VERTICAL){
		int cur_index = vertical_change[x][y];
		int new_index;
		if(x + 3 > n || board[x + 1][y] != 'G' || board[x + 2][y] != 'W') return;
		// 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(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(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);
			}
		}
	}else{
		int cur_index = horizontal_change[x][y];
		int new_index;
		if(y + 3 > m || board[x][y + 1] != 'G' || board[x][y + 2] != 'W') return;
		// 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(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(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);
			}
		}
	}
	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'){
				vertical_parent[i][j] = make_pair(i, j);
				horizontal_parent[i][j] = make_pair(i, j);
				// horizontal:
				if(j + 2 <= m && board[i][j + 1] == 'G' && board[i][j + 2] == 'W'){
					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_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 <= n; j++){
			if(board[i][j] == 'R'){
				build(i, j, VERTICAL);
				build(i, j, HORIZONTAL);
				if(vertical_check[i][j] && horizontal_check[i][j]){
					G[vertical_change[i][j]].push_back(horizontal_change[i][j]);
					G[horizontal_change[i][j]].push_back(vertical_change[i][j]);
				}
			}
		}
	}

	int result = 0;
	for(int i = 1; i <= cnt; i++){
		if(!check[i]){
			result += bfs(i);
		}
	}

	cout << result << endl;

	return 0;
}

Compilation message

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 time Memory Grader output
1 Correct 99 ms 235084 KB Output is correct
2 Correct 103 ms 235148 KB Output is correct
3 Correct 104 ms 235180 KB Output is correct
4 Correct 103 ms 235084 KB Output is correct
5 Correct 116 ms 235200 KB Output is correct
6 Incorrect 107 ms 235176 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 99 ms 235084 KB Output is correct
2 Correct 103 ms 235148 KB Output is correct
3 Correct 104 ms 235180 KB Output is correct
4 Correct 103 ms 235084 KB Output is correct
5 Correct 116 ms 235200 KB Output is correct
6 Incorrect 107 ms 235176 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 99 ms 235084 KB Output is correct
2 Correct 103 ms 235148 KB Output is correct
3 Correct 104 ms 235180 KB Output is correct
4 Correct 103 ms 235084 KB Output is correct
5 Correct 116 ms 235200 KB Output is correct
6 Incorrect 107 ms 235176 KB Output isn't correct
7 Halted 0 ms 0 KB -