結果
| 問題 |
No.307 最近色塗る問題多くない?
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2020-12-23 17:53:04 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,749 bytes |
| コンパイル時間 | 2,277 ms |
| コンパイル使用メモリ | 79,904 KB |
| 実行使用メモリ | 76,944 KB |
| 最終ジャッジ日時 | 2024-09-21 16:28:04 |
| 合計ジャッジ時間 | 18,854 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 13 WA * 23 |
ソースコード
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int h = sc.nextInt();
int w = sc.nextInt();
boolean[][] field = new boolean[h][w];
UnionFindTree uft = new UnionFindTree(h * w);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
field[i][j] = (sc.nextInt() == 1);
uft.setColor(i * w + j, field[i][j]);
if (j > 0) {
if (field[i][j] == field[i][j - 1]) {
uft.unite(i * w + j, i * w + j - 1);
} else {
uft.addNeighbour(i * w + j, i * w + j - 1);
uft.addNeighbour(i * w + j - 1, i * w + j);
}
}
if (i > 0) {
if (field[i][j] == field[i - 1][j]) {
uft.unite(i * w + j, (i - 1) * w + j);
} else {
uft.addNeighbour(i * w + j, (i - 1) * w + j);
uft.addNeighbour((i - 1) * w + j, i * w + j);
}
}
}
}
int q = sc.nextInt();
for (int i = 0; i < q; i++) {
int r = sc.nextInt() - 1;
int c = sc.nextInt() - 1;
boolean color = (sc.nextInt() == 1);
if (uft.getColor(r * w + c) == color) {
continue;
}
uft.uniteColor(r * w + c, color);
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (j > 0) {
sb.append(" ");
}
if (uft.getColor(i * w + j)) {
sb.append("1");
} else {
sb.append("0");
}
}
sb.append("\n");
}
System.out.print(sb);
}
static class UnionFindTree {
int[] paretns;
ArrayList<HashSet<Integer>> neighbours;
boolean[] colors;
public UnionFindTree(int size) {
paretns = new int[size];
neighbours = new ArrayList<>();
colors = new boolean[size];
for (int i = 0; i < size; i++) {
paretns[i] = i;
neighbours.add(new HashSet<>());
}
}
public int find(int x) {
if (x == paretns[x]) {
return x;
} else {
return paretns[x] = find(paretns[x]);
}
}
public boolean same(int x, int y) {
return find(x) == find(y);
}
public void addNeighbour(int x, int y) {
int xx = find(x);
int yy = find(y);
if (xx == yy) {
return;
}
neighbours.get(xx).add(yy);
}
public void unite(int x, int y) {
int xx = find(x);
int yy = find(y);
if (xx == yy) {
return;
}
for (int zz : neighbours.get(xx)) {
addNeighbour(yy, xx);
}
paretns[xx] = yy;
}
public void setColor(int x, boolean c) {
colors[find(x)] = c;
}
public boolean getColor(int x) {
return colors[find(x)];
}
public void uniteColor(int x, boolean c) {
HashSet<Integer> next = new HashSet<>();
for (int y : neighbours.get(find(x))) {
unite(x, y);
}
}
}
}
tenten