結果
問題 | No.2563 色ごとのグループ |
ユーザー | tenten |
提出日時 | 2023-12-04 16:00:27 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,086 ms / 2,000 ms |
コード長 | 3,011 bytes |
コンパイル時間 | 3,519 ms |
コンパイル使用メモリ | 92,948 KB |
実行使用メモリ | 90,040 KB |
最終ジャッジ日時 | 2024-09-26 23:03:14 |
合計ジャッジ時間 | 19,046 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 62 ms
36,980 KB |
testcase_01 | AC | 61 ms
36,644 KB |
testcase_02 | AC | 61 ms
36,568 KB |
testcase_03 | AC | 61 ms
36,760 KB |
testcase_04 | AC | 62 ms
36,712 KB |
testcase_05 | AC | 61 ms
36,980 KB |
testcase_06 | AC | 62 ms
36,812 KB |
testcase_07 | AC | 61 ms
36,580 KB |
testcase_08 | AC | 62 ms
36,972 KB |
testcase_09 | AC | 65 ms
36,976 KB |
testcase_10 | AC | 61 ms
36,976 KB |
testcase_11 | AC | 64 ms
37,044 KB |
testcase_12 | AC | 65 ms
36,976 KB |
testcase_13 | AC | 63 ms
36,848 KB |
testcase_14 | AC | 104 ms
38,992 KB |
testcase_15 | AC | 124 ms
39,388 KB |
testcase_16 | AC | 112 ms
39,268 KB |
testcase_17 | AC | 117 ms
39,488 KB |
testcase_18 | AC | 89 ms
38,312 KB |
testcase_19 | AC | 153 ms
40,648 KB |
testcase_20 | AC | 215 ms
45,180 KB |
testcase_21 | AC | 177 ms
44,540 KB |
testcase_22 | AC | 169 ms
42,424 KB |
testcase_23 | AC | 189 ms
43,784 KB |
testcase_24 | AC | 592 ms
57,416 KB |
testcase_25 | AC | 638 ms
64,612 KB |
testcase_26 | AC | 869 ms
71,796 KB |
testcase_27 | AC | 1,081 ms
90,040 KB |
testcase_28 | AC | 820 ms
71,752 KB |
testcase_29 | AC | 957 ms
88,056 KB |
testcase_30 | AC | 1,086 ms
88,240 KB |
testcase_31 | AC | 1,033 ms
88,580 KB |
testcase_32 | AC | 1,048 ms
88,488 KB |
testcase_33 | AC | 653 ms
52,724 KB |
testcase_34 | AC | 634 ms
52,284 KB |
testcase_35 | AC | 681 ms
52,364 KB |
testcase_36 | AC | 642 ms
52,796 KB |
testcase_37 | AC | 627 ms
51,704 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int m = sc.nextInt(); int[] colors = new int[n]; for (int i = 0; i < n; i++) { colors[i] = sc.nextInt(); } UnionFindTree uft = new UnionFindTree(n); for (int i = 0; i < m; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; if (colors[a] == colors[b]) { uft.unite(a, b); } } HashMap<Integer, HashSet<Integer>> parents = new HashMap<>(); for (int i = 0; i < n; i++) { if (!parents.containsKey(colors[i])) { parents.put(colors[i], new HashSet<>()); } parents.get(colors[i]).add(uft.find(i)); } int ans = 0; for (HashSet<Integer> x : parents.values()) { ans += x.size() - 1; } System.out.println(ans); } static class UnionFindTree { int[] parents; public UnionFindTree(int size) { parents = new int[size]; for (int i = 0; i < size; i++) { parents[i] = i; } } public int find(int x) { if (x == parents[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) { if (!same(x, y)) { parents[find(x)] = find(y); } } } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }