結果
問題 | No.1266 7 Colors |
ユーザー |
![]() |
提出日時 | 2020-11-20 09:28:56 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 2,200 ms / 3,000 ms |
コード長 | 2,645 bytes |
コンパイル時間 | 2,494 ms |
コンパイル使用メモリ | 79,492 KB |
実行使用メモリ | 101,836 KB |
最終ジャッジ日時 | 2024-07-23 11:59:24 |
合計ジャッジ時間 | 44,127 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
import java.util.*;public class Main {public static void main (String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();int m = sc.nextInt();int q = sc.nextInt();UnionFindTree uft = new UnionFindTree(n * 7);boolean[][] colors = new boolean[n][7];for (int i = 0; i < n; i++) {char[] arr = sc.next().toCharArray();for (int j = 0; j < 7; j++) {colors[i][j] = (arr[j] == '1');}for (int j = 0; j < 7; j++) {if (colors[i][j] && colors[i][(j + 1) % 7]) {uft.unite(i * 7 + j, i * 7 + (j + 1) % 7);}}}ArrayList<ArrayList<Integer>> graph = new ArrayList<>();for (int i = 0; i < n; i++) {graph.add(new ArrayList<>());}for (int i = 0; i < m; i++) {int a = sc.nextInt() - 1;int b = sc.nextInt() - 1;graph.get(a).add(b);graph.get(b).add(a);;for (int j = 0; j < 7; j++) {if (colors[a][j] && colors[b][j]) {uft.unite(a * 7 + j, b * 7 + j);}}}StringBuilder sb = new StringBuilder();for (int i = 0; i < q; i++) {int type = sc.nextInt();int a = sc.nextInt() - 1;int b = sc.nextInt() - 1;if (type == 1) {colors[a][b] = true;if (colors[a][(b - 1 + 7) % 7]) {uft.unite(a * 7 + b, a * 7 + (b - 1 + 7) % 7);}if (colors[a][(b + 1) % 7]) {uft.unite(a * 7 + b, a * 7 + (b + 1) % 7);}for (int x : graph.get(a)) {if (colors[x][b]) {uft.unite(a * 7 + b, x * 7 + b);}}} else {sb.append(uft.counts[uft.find(a * 7)]).append("\n");}}System.out.print(sb);}static class UnionFindTree {int[] parents;int[] counts;public UnionFindTree(int size) {parents = new int[size];counts = new int[size];for (int i = 0; i < size; i++) {parents[i] = i;counts[i] = 1;}}public int find(int x) {if (parents[x] == x) {return x;} else {return parents[x] = find(parents[x]);}}public boolean same(int x, int y) {return find(x) == find(y);}public void unite(int x, int y) {int xx = find(x);int yy = find(y);if (xx == yy) {return;}parents[xx] = yy;counts[yy] += counts[xx];}}}