답안 #345659

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
345659 2021-01-07T19:22:04 Z pggp Paint (COI20_paint) C++14
17 / 100
3000 ms 124592 KB
#include <bits/stdc++.h>

using namespace std;

int R, S;
int parent[250000];
int siz[250000];

bool db = false;

int find(int x){
	if(parent[x] == x){
		return x;
	}
	return parent[x] = find(parent[x]);
}

void un(int x, int y){
	parent[find(x)] = find(y);
}



vector < int > E[250000];
int col[250000];
bool active[250000];

void union_vertices(int x, int y){
	if(!active[x] or !active[y])	return;
	//cout << "UNION " << x << " " << y << endl;
	un(x, y);
	set < int > S;
	vector < int > v;
	vector < int > v1;
	for(int ver : E[y]){
		if(S.find(ver) == S.end() and active[ver]){
			S.insert(ver);
			v.push_back(ver);
		}
	}
	for(int ver : E[x]){
		if(S.find(ver) == S.end() and active[ver]){
			S.insert(ver);
			v1.push_back(ver);
		}
	}
	E[x].clear();
	active[x] = false;
	E[y].clear();
	for(int ver : v){
		//cout << "ver = " << ver << endl;
		E[y].push_back(ver);
	}
	for(int ver : v1){
		if(ver == y)	continue;
		//cout << "ver = " << ver << endl;
		E[y].push_back(ver);
		E[ver].push_back(y);
	}
}

bool used[250000];
stack < int > us;


void color(int v, int c){
	//cout << v << ":" << c << endl;
	col[v] = c;
	vector < int > t = E[v];
	for(int ver : t){
		if(col[ver] == c){
			union_vertices(ver, v);
		}
	}
}



void print_output(){
	for (int y = 0; y < R; ++y)
	{
		for (int x = 0; x < S; ++x)
		{
			cout << col[find(y * S + x)] << " ";
			if(db)	cout << "(" << find(y * S + x) << ")";
		}
		cout << endl;
	}
	if(db)
	{
		for(int i = 0; i < R * S; i++){
		cout << i << ": ";
		for(int v : E[i]){
			cout << v << " ";
		}
		cout << endl;
		}
	}
}

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(false);

	cin >> R >> S;
	for (int i = 0; i < R * S; ++i)
	{
		active[i] = true;
		col[i] = 100001 + i;
		parent[i] = i;
		if(i % S != S - 1){
			E[i].push_back(i + 1);
		}
		if(i % S != 0){
			E[i].push_back(i - 1);
		}
		if(i >= S){
			E[i].push_back(i - S);
		}
		if(i < R*S - S){
			E[i].push_back(i + S);
		}
	}

	for (int y = 0; y < R; ++y)
	{
		for (int x = 0; x < S; ++x)
		{
			int c;
			cin >> c;
			color(find(y * S + x), c);
			//print_output();
			//cout << endl << endl;
		}
	}

	int q;

	cin >> q;
	while(q--){
		int r, s, c;
		cin >> r >> s >> c;
		r--;
		s--;
		if(db)	print_output();
		if(db)	cout << endl << endl;
		color(find(r * S + s), c);
			//print_output();
			//cout << endl << endl;
	}
	
	print_output();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6252 KB Output is correct
2 Correct 5 ms 6380 KB Output is correct
3 Correct 9 ms 6636 KB Output is correct
4 Correct 32 ms 6892 KB Output is correct
5 Correct 2866 ms 124592 KB Output is correct
6 Correct 443 ms 11756 KB Output is correct
7 Correct 4 ms 6252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 8428 KB Output is correct
2 Correct 122 ms 11116 KB Output is correct
3 Correct 166 ms 18148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3087 ms 17320 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3078 ms 16108 KB Time limit exceeded
2 Halted 0 ms 0 KB -