답안 #690037

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
690037 2023-01-30T03:34:45 Z sugartheanh Paint (COI20_paint) C++14
0 / 100
6 ms 596 KB
#include <bits/stdc++.h>
using namespace std;

const int D[] = {0, 1, 0, -1};
const int MAGIC = 512;

int main() {
    freopen("new.inp","r",stdin);
    freopen("new.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

	int n, m; cin >> n >> m;

	vector<int> color(n * m);
	for (auto &i : color) cin >> i;

    vector<vector<int>> neighbors(n * m);
	vector<vector<int>> members(n * m);
	vector<int> leader(n * m);

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			int id = i * m + j;
			leader[id] = id;
			members[id].push_back(id);
			for (int dir = 0; dir < 4; ++dir) {
				int i_new = i + D[dir], j_new = j + D[(dir + 1) % 4];
				if (0 <= i_new && i_new < n && 0 <= j_new && j_new < m) {
                    neighbors[id].push_back(i_new * m + j_new);
				}
			}
		}
	}

	vector<vector<set<int>>> neighbors_by_color(n * m);
    vector<bool> in_vector(n * m);
    set<int> big_groups;

//    auto normalize = [&](int a) {
//        vector<int> neighbors_new;
//        for (auto b : neighbors[a]) {
//            b = leader[b];
//            if (leader[b] != a && !in_vector[b]) {
//                neighbors_new.push_back(b);
//                in_vector[b] = true;
//            }
//        }
//        swap(neighbors[a], neighbors_new);
//    };
//
//	auto update = [&](int a) {
//         {
//            normalize(a);
//            for (auto b : neighbors[a]) in_vector[b] = false;
//        }
//	};

	auto merge = [&](int a, int b) {
		if (members[a].size() < members[b].size()) swap(a, b);

		for (auto i : members[b]) leader[i] = a;

		members[a].insert(members[a].end(), members[b].begin(), members[b].end());
		members[b].clear();

		neighbors[a].insert(neighbors[a].end(), neighbors[b].begin(), neighbors[b].end());
		neighbors[b].clear();
	};

	auto fill = [&](int a, int c) {
		a = leader[a];

        while (true) {
            {
                bool merged = false;
                auto initial_neighbors = neighbors[a];
                for (auto b : initial_neighbors) {
                    b = leader[b];
                    if (b != a && color[b] == c) {
                        merged = true;
                        merge(a, b);
                        a = leader[a];
                    }
                }
                if (!merged) break;
            }
        }

		color[a] = c;
//		update(a);
	};

	for (int i = 0; i < n * m; ++i)
		fill(i, color[i]);

	int q; cin >> q;

	while (q--) {
		int i, j, c; cin >> i >> j >> c; --i, --j;
		fill(i * m + j, c);
	}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cout << color[leader[i * m + j]] << " ";
		}
		cout << "\n";
	}
}

Compilation message

paint.cpp: In function 'int main()':
paint.cpp:8:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 |     freopen("new.inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
paint.cpp:9:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |     freopen("new.out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 596 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 596 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 596 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 596 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -