답안 #774479

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
774479 2023-07-05T18:38:02 Z vjudge1 Paint (COI20_paint) C++17
0 / 100
61 ms 36048 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


const int S = 450;
int root[N], col[N], sz[N];
vector<vector<int>> edges[N];
vector<int> p[N];
vector<pii> dir, inv;
vector<int> large[N];
vector<int> ind[N];


int find(int node){
	if (root[node] == node) return node;
	return root[node] = find(root[node]);
}


void uni(int a,int b){
	a = find(a), b = find(b);
	if (a == b) return;
	if (sz[a] < sz[b]) swap(a, b);
	root[b] = a;
	for (auto i : p[b]) p[a].pb(i);
	
	if (sz[a] < S && sz[a] + sz[b] >= S){
		//cout<<1<<endl;
		edges[a].resize(N);
		for (auto i : p[a]){
			int x = inv[i].st, y = inv[i].nd;
			for (auto k : dir){
				int w = x + k.st, z = y + k.nd;
				int tmp = col[find(ind[w][z])];
				if (tmp != col[a]) 
				{
					edges[a][tmp].pb(find(ind[w][z]));  
					large[find(ind[w][z])].pb(a);                                                                                                                                                                                                                                                                                                                                                                                                                                     
				}
			}
		}
	}

	
	else if (sz[a] >= S && sz[b] < S){
		//cout<<2<<endl;
		for (auto i : p[b]){
			int x = inv[i].st, y = inv[i].nd;
			for (auto k : dir){
				int w = x + k.st, z = y + k.nd;
				int tmp = col[find(ind[w][z])];
				if (tmp != col[a]) 
				{
					edges[a][tmp].pb(find(ind[w][z]));  
					large[find(ind[w][z])].pb(a);                                                                                                                                                                                                                                                                                                                                                                                                                                     
				}
			}
		}
	}
	
	else if (sz[b] >= S){
		//cout<<3<<endl;
		for (int i = 1; i < N; i++){
			if (edges[a][i].size() < edges[b][i].size()) edges[a][i].swap(edges[b][i]);
			for (auto j : edges[b][i])
				edges[a][i].pb(j);
		}
		edges[b].clear();
	}
	sz[a] += sz[b];
}


void change_color(int node, int c){
	node = find(node);
	if (sz[node] >= S){
		vector<int> tmp = edges[node][c];
		for (auto j : tmp){
			if (col[find(j)] != c) continue;
			uni(node, j);
			col[find(node)] = c;
			node = find(node);
		}
		edges[node][c].clear();
	}
	else{
		vector<int> tmp = p[node];
		for (auto i : tmp){
			int x = inv[i].st, y = inv[i].nd;
			for (auto k : dir){
				int w = x + k.st, z = y + k.nd;
				int tmp = col[find(ind[w][z])];
				if (tmp == c) 
				{
					uni(find(ind[w][z]), node);
				}
			}
		}
		for (auto i : large[node])
			edges[i][c].pb(find(node));

	}
	col[find(node)] = c;
}


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

	dir.pb({0, 1});
	dir.pb({1, 0});

	int cntr = 1;
	int r, s;
	cin>>r>>s;
	int arr[r + 5][s + 5];
	memset(arr, -1, sizeof(arr));
	inv.pb({0, 0});
	for (int i = 1; i <= r; i++){
		ind[i].resize(s + 5, 0);
		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++;
			p[cntr].pb(cntr);
			inv.pb({i, j});
		}
	}

	ind[r + 1].resize(s + 5, 0);
	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]);
			}
		}
	}


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

//	cout<<r<<sp<<s<<endl;
	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 12 ms 19156 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 45 ms 23256 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 56 ms 36048 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 61 ms 28156 KB Output isn't correct
2 Halted 0 ms 0 KB -