結果

問題 No.1266 7 Colors
ユーザー tentententen
提出日時 2020-11-20 09:28:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,200 ms / 3,000 ms
コード長 2,645 bytes
コンパイル時間 5,769 ms
コンパイル使用メモリ 76,948 KB
実行使用メモリ 87,636 KB
最終ジャッジ日時 2023-09-30 18:19:30
合計ジャッジ時間 44,766 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
39,532 KB
testcase_01 AC 128 ms
39,740 KB
testcase_02 AC 129 ms
39,640 KB
testcase_03 AC 1,776 ms
61,592 KB
testcase_04 AC 2,108 ms
73,440 KB
testcase_05 AC 1,976 ms
61,836 KB
testcase_06 AC 1,955 ms
78,076 KB
testcase_07 AC 1,968 ms
80,596 KB
testcase_08 AC 2,200 ms
77,328 KB
testcase_09 AC 2,140 ms
70,356 KB
testcase_10 AC 2,154 ms
69,616 KB
testcase_11 AC 2,127 ms
65,480 KB
testcase_12 AC 2,091 ms
66,932 KB
testcase_13 AC 2,130 ms
67,784 KB
testcase_14 AC 1,903 ms
61,968 KB
testcase_15 AC 2,075 ms
82,008 KB
testcase_16 AC 2,112 ms
67,092 KB
testcase_17 AC 2,153 ms
80,960 KB
testcase_18 AC 1,721 ms
87,636 KB
testcase_19 AC 1,760 ms
86,444 KB
testcase_20 AC 1,734 ms
87,432 KB
testcase_21 AC 1,379 ms
64,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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];
       }
   }
}

0