結果
| 問題 |
No.307 最近色塗る問題多くない?
|
| コンテスト | |
| ユーザー |
tnakao0123
|
| 提出日時 | 2016-04-02 23:45:00 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 223 ms / 4,000 ms |
| コード長 | 3,433 bytes |
| コンパイル時間 | 894 ms |
| コンパイル使用メモリ | 91,288 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-11-21 22:16:50 |
| 合計ジャッジ時間 | 3,668 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 36 |
ソースコード
/* -*- coding: utf-8 -*-
*
* 307.cc: No.307 最近色塗る問題多くない? - yukicoder
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
/* constant */
const int MAX_H = 200;
const int MAX_W = 200;
const int MAX_N = MAX_H * MAX_W;
const int dxs[] = {1, 0, -1, 0}, dys[] = {0, -1, 0, 1};
/* typedef */
typedef queue<int> qi;
struct UFT {
int links[MAX_N], ranks[MAX_N], sizes[MAX_N];
void clear(int n) {
for (int i = 0; i < n; i++) links[i] = i, ranks[i] = sizes[i] = 1;
}
UFT() {}
UFT(int n) { clear(n); }
int root(int i) {
int i0 = i;
while (links[i0] != i0) i0 = links[i0];
return (links[i] = i0);
}
int rank(int i) { return ranks[root(i)]; }
int size(int i) { return sizes[root(i)]; }
bool same(int i, int j) { return root(i) == root(j); }
int merge(int i0, int i1) {
int r0 = root(i0), r1 = root(i1), mr;
if (r0 == r1) return r0;
if (ranks[r0] == ranks[r1]) {
links[r1] = r0;
sizes[r0] += sizes[r1];
ranks[r0]++;
mr = r0;
}
else if (ranks[r0] > ranks[r1]) {
links[r1] = r0;
sizes[r0] += sizes[r1];
mr = r0;
}
else {
links[r0] = r1;
sizes[r1] += sizes[r0];
mr = r1;
}
return mr;
}
};
/* global variables */
int h, w, n;
bool as[MAX_N], used[MAX_N];
UFT uft;
/* subroutines */
void printflds(bool bs[]) {
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if (x) putchar(' ');
printf("%d", bs[uft.root(y * w + x)]);
}
putchar('\n');
}
}
/* main */
int main() {
cin >> h >> w;
n = h * w;
for (int i = 0; i < n; i++) cin >> as[i];
//printflds(as);
uft.clear(n);
for (int y = 0, pos = 0; y < h; y++)
for (int x = 0; x < w; x++, pos++)
if (! used[pos]) {
bool c = as[pos];
qi q;
q.push(pos);
while (! q.empty()) {
int u = q.front(); q.pop();
int ux = u % w, uy = u / w;
for (int di = 0; di < 4; di++) {
int vx = ux + dxs[di], vy = uy + dys[di];
if (vx >= 0 && vx < w && vy >= 0 && vy < h) {
int v = vy * w + vx;
if (! used[v] && as[v] == c) {
used[v] = true;
q.push(v);
if (! uft.same(u, v)) uft.merge(u, v);
}
}
}
}
}
for (int y = 0; false && y < h; y++) {
for (int x = 0; x < w; x++) printf("%2d ", uft.root(y * w + x));
putchar('\n');
}
int q;
cin >> q;
while (q--) {
int yi, xi;
bool ci;
cin >> yi >> xi >> ci;
yi--, xi--;
int p = yi * w + xi, rp = uft.root(p);
if (as[rp] == ci) continue;
if (uft.size(rp) == n) {
as[rp] = ci;
continue;
}
memset(used, false, sizeof(used));
used[p] = true;
qi q;
q.push(p);
while (! q.empty()) {
int u = q.front(); q.pop();
int ux = u % w, uy = u / w;
for (int di = 0; di < 4; di++) {
int vx = ux + dxs[di], vy = uy + dys[di];
if (vx >= 0 && vx < w && vy >= 0 && vy < h) {
int v = vy * w + vx;
if (! used[v] && as[uft.root(v)] != ci) {
used[v] = true;
q.push(v);
if (! uft.same(u, v)) uft.merge(u, v);
}
}
}
}
as[uft.root(p)] = ci;
}
printflds(as);
return 0;
}
tnakao0123