結果

問題 No.307 最近色塗る問題多くない?
ユーザー pekempeypekempey
提出日時 2015-11-28 00:12:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3,051 ms / 4,000 ms
コード長 2,270 bytes
コンパイル時間 1,832 ms
コンパイル使用メモリ 168,312 KB
実行使用メモリ 16,264 KB
最終ジャッジ日時 2024-05-01 16:42:11
合計ジャッジ時間 9,350 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,448 KB
testcase_01 AC 4 ms
8,320 KB
testcase_02 AC 3 ms
8,344 KB
testcase_03 AC 3 ms
8,448 KB
testcase_04 AC 4 ms
8,576 KB
testcase_05 AC 4 ms
8,448 KB
testcase_06 AC 3 ms
8,448 KB
testcase_07 AC 10 ms
8,832 KB
testcase_08 AC 63 ms
10,368 KB
testcase_09 AC 34 ms
9,344 KB
testcase_10 AC 11 ms
8,576 KB
testcase_11 AC 27 ms
9,088 KB
testcase_12 AC 4 ms
8,372 KB
testcase_13 AC 14 ms
8,576 KB
testcase_14 AC 6 ms
8,704 KB
testcase_15 AC 9 ms
8,704 KB
testcase_16 AC 7 ms
8,704 KB
testcase_17 AC 29 ms
9,984 KB
testcase_18 AC 77 ms
12,288 KB
testcase_19 AC 95 ms
12,928 KB
testcase_20 AC 7 ms
8,576 KB
testcase_21 AC 119 ms
15,744 KB
testcase_22 AC 957 ms
15,360 KB
testcase_23 AC 90 ms
15,872 KB
testcase_24 AC 93 ms
15,940 KB
testcase_25 AC 121 ms
15,572 KB
testcase_26 AC 107 ms
15,872 KB
testcase_27 AC 88 ms
16,000 KB
testcase_28 AC 3,051 ms
16,092 KB
testcase_29 AC 10 ms
8,320 KB
testcase_30 AC 105 ms
16,256 KB
testcase_31 AC 125 ms
16,256 KB
testcase_32 AC 37 ms
16,264 KB
testcase_33 AC 397 ms
10,368 KB
testcase_34 AC 405 ms
9,856 KB
testcase_35 AC 407 ms
10,332 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:69:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   69 |         rep (i, h) rep (j, w) scanf("%d", &A[i][j]);
      |                               ~~~~~^~~~~~~~~~~~~~~~
main.cpp:91:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   91 |                 scanf("%d %d %d", &r, &c, &x);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
#define GET_MACRO(a, b, c, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define rep2(i, a) rep3 (i, 0, a)
#define rep3(i, a, b) for (int i = (a); i < (b); i++)
#define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__)
#define repr2(i, a) repr3 (i, 0, a)
#define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define chmin(a, b) ((b) < a && (a = (b), true))
#define chmax(a, b) (a < (b) && (a = (b), true))
using namespace std;
typedef long long ll;

int size;
int parent[101010], color[101010];
set<int> g[101010];

void init(int n) {
	size = n;
    rep (i, n) parent[i] = i;
}
int root(int x) {
    if (x == parent[x]) return x;
    else return parent[x] = root(parent[x]);
}
void merge(int x, int y) {
    x = root(x); y = root(y);
    if (x == y) return;
    if (g[x].size() < g[y].size()) swap(x, y);
    parent[y] = x;
    g[x].insert(g[y].begin(), g[y].end());
    g[y].clear();

    auto it = g[x].begin();
    while (it != g[x].end()) {
        if (root(*it) == x) {
            g[x].erase(it++);
        } else ++it;
    }
}
bool same(int x, int y) {
    return root(x) == root(y);
}
void add(int u, int v) {
    u = root(u); v = root(v);
    if (u == v) return;
    g[u].insert(v);
    g[v].insert(u);
}
int getcolor(int x) {
    x = root(x);
    return color[x];
}
void update(int x, int c) {
    x = root(x);
    if (color[x] == c) return;
    color[x] = c;
    vector<int> a(g[x].begin(), g[x].end());
    for (int y : a) if (getcolor(y) == c) {
        merge(x, y);
    }
}
int A[200][200];

int main() {
	int h, w;
	cin >> h >> w;
	init(h * w);
	rep (i, h) rep (j, w) scanf("%d", &A[i][j]);
	rep (i, h) rep (j, w) color[i * w + j] = A[i][j];
	rep (i, h) rep (j, w) {
		int dy[] = {1, 0};
		int dx[] = {0, 1};
		rep (k, 2) {
			int ny = i + dy[k];
			int nx = j + dx[k];
			if (ny < h && nx < w) {
				if (A[ny][nx] == A[i][j]) {
					merge(ny * w + nx, i * w + j);
				} else {
					add(ny * w + nx, i * w + j);
				}
			}
		}
	}

	int q;
	cin >> q;
	rep (i, q) {
		int r, c, x;
		scanf("%d %d %d", &r, &c, &x);
		r--; c--;
		update(r * w + c, x);
	}

	rep (i, h) {
		rep (j, w) {
			if (j) printf(" ");
			printf("%d", getcolor(i * w + j));
		}
		printf("\n");
	}
	return 0;
}
0