답안 #773778

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
773778 2023-07-05T08:26:42 Z vjudge1 Paint (COI20_paint) C++17
31 / 100
3000 ms 20280 KB
#include <bits/stdc++.h>
using namespace std;
#define sp " "
//#define endl "\n"
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define N 200005

int root[N], col[N], sz[N];
vector<int> edges[N];

int find(int node){
	//cout<<"! "<<node<<sp<<root[node]<<endl;
	if (root[node] == node) return node;
	return root[node] = find(root[node]);
}


void uni(int a,int b){
	a = find(a), b = find(b);
	//cout<<a<<sp<<b<<endl;
	if (a == b) return;
	if (sz[a] < sz[b]) swap(a, b);
	if (edges[a].size() < edges[b].size()) edges[a].swap(edges[b]);
	root[b] = a;
	sz[a] += sz[b];
	for (auto i : edges[b]) if (find(i) != a) edges[a].pb(i);
}


void change_color(int node, int c){
	node = find(node);
	int prv = col[node];
	if (prv == c) return;
	vector<int> merge = edges[node];
	for (auto i : merge) uni(i, node);
	col[find(node)] = 1 ^ prv;
}


int32_t main(){
	//fileio();
	fastio();

	vector<pii> dir;
	dir.pb({0, 1});
	dir.pb({1, 0});

	int cntr = 1;
	int r, s;
	cin>>r>>s;
	int arr[r + 5][s + 5], ind[r + 5][s + 5];
	memset(arr, -1, sizeof(arr));
	memset(ind, 0, sizeof(ind));
	for (int i = 1; i <= r; i++){
		for (int j = 1; j <= s; j++)
		{
			cin>>arr[i][j];
			root[cntr] = cntr;
			sz[cntr] = 1;
			col[cntr] = arr[i][j];
			ind[i][j] = cntr;
			cntr++;
			//cout<<i<<sp<<j<<sp<<ind[i][j]<<endl;
		}
	}


	for (int i = 1; i <= r; i++)
	{
		for (int j = 1; j <= s; j++){
			for (auto k : dir){
				int a = i + k.st, b = j + k.nd;
				if (ind[a][b] == 0) continue;
				if (arr[a][b] == arr[i][j]) uni(ind[a][b], ind[i][j]);
				else {
					int u = find(ind[i][j]), v = find(ind[a][b]);
					edges[u].pb(v);
					edges[v].pb(u);
				}
			}
		}
	}


	int q;
	cin>>q;
	while(q--){
		int r, c, x;
		cin>>r>>c>>x;
		change_color(ind[r][c], x);
	}

	for (int i = 1; i <= r; i++){
		for(int j = 1; j <= s; j++)
			cout<<col[find(ind[i][j])]<<sp;
		cout<<endl;
	}

	//cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 5076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 249 ms 10840 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 906 ms 20280 KB Output is correct
2 Correct 191 ms 18928 KB Output is correct
3 Correct 227 ms 19672 KB Output is correct
4 Correct 909 ms 20148 KB Output is correct
5 Correct 2298 ms 19604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3041 ms 15328 KB Time limit exceeded
2 Halted 0 ms 0 KB -