結果
問題 | No.2563 色ごとのグループ |
ユーザー | 👑 SPD_9X2 |
提出日時 | 2024-02-03 01:07:10 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,461 ms / 2,000 ms |
コード長 | 2,684 bytes |
コンパイル時間 | 2,412 ms |
コンパイル使用メモリ | 80,280 KB |
実行使用メモリ | 78,928 KB |
最終ジャッジ日時 | 2024-02-03 01:07:36 |
合計ジャッジ時間 | 26,335 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 126 ms
57,812 KB |
testcase_01 | AC | 129 ms
57,840 KB |
testcase_02 | AC | 126 ms
57,560 KB |
testcase_03 | AC | 129 ms
57,816 KB |
testcase_04 | AC | 132 ms
57,704 KB |
testcase_05 | AC | 126 ms
58,220 KB |
testcase_06 | AC | 127 ms
57,852 KB |
testcase_07 | AC | 129 ms
57,692 KB |
testcase_08 | AC | 131 ms
57,832 KB |
testcase_09 | AC | 172 ms
57,960 KB |
testcase_10 | AC | 135 ms
57,824 KB |
testcase_11 | AC | 169 ms
57,956 KB |
testcase_12 | AC | 146 ms
57,840 KB |
testcase_13 | AC | 137 ms
57,952 KB |
testcase_14 | AC | 196 ms
60,180 KB |
testcase_15 | AC | 198 ms
60,220 KB |
testcase_16 | AC | 198 ms
60,464 KB |
testcase_17 | AC | 201 ms
60,260 KB |
testcase_18 | AC | 197 ms
58,208 KB |
testcase_19 | AC | 249 ms
62,032 KB |
testcase_20 | AC | 338 ms
62,600 KB |
testcase_21 | AC | 283 ms
62,544 KB |
testcase_22 | AC | 277 ms
60,776 KB |
testcase_23 | AC | 325 ms
62,644 KB |
testcase_24 | AC | 895 ms
70,992 KB |
testcase_25 | AC | 774 ms
74,404 KB |
testcase_26 | AC | 1,001 ms
75,592 KB |
testcase_27 | AC | 967 ms
78,700 KB |
testcase_28 | AC | 1,065 ms
75,440 KB |
testcase_29 | AC | 1,156 ms
77,008 KB |
testcase_30 | AC | 1,238 ms
77,232 KB |
testcase_31 | AC | 1,165 ms
78,316 KB |
testcase_32 | AC | 1,184 ms
78,928 KB |
testcase_33 | AC | 1,257 ms
72,488 KB |
testcase_34 | AC | 1,277 ms
73,432 KB |
testcase_35 | AC | 1,251 ms
72,752 KB |
testcase_36 | AC | 1,286 ms
72,568 KB |
testcase_37 | AC | 1,461 ms
75,180 KB |
ソースコード
import java.io.PrintWriter; import java.util.*; public class Main { public static void vectorPrint(Vector lis){ PrintWriter out = new PrintWriter(System.out); for (int i=0 ; i<lis.size() ; i++){ out.print(lis.get(i)); if ( i != lis.size()-1){ out.print(' '); }else{ out.print('\n'); } } out.flush(); } public static void main(String[] args){ Scanner sc = new Scanner(System.in); int N = Integer.parseInt(sc.next()); int M = Integer.parseInt(sc.next()); int[] C = new int[N]; for (int i=0; i<N ;i++){ C[i] = Integer.parseInt(sc.next()); } Dsu uf = new Dsu(N); for (int i=0; i<M ; i++){ int u = Integer.parseInt(sc.next()); int v = Integer.parseInt(sc.next()); u--; v--; if (C[u] == C[v]){ uf.merge(u, v); } } Map<Integer,Integer> dic = new HashMap<>(); for (int i=0; i<N ; i++){ if (uf.leader(i) == i){ if (!dic.containsKey(C[i])){ dic.put(C[i], 0); } dic.put(C[i], dic.get(C[i]) + 1); } } int ans = 0; for (Map.Entry<Integer,Integer> tup : dic.entrySet()){ ans += tup.getValue(); ans -= 1; } System.out.println(ans); } } class Dsu { int n; int[] p; int[] s; Dsu(int N) { this.n = N; this.p = new int[N]; this.s = new int[N]; for (int i=0; i<N ; i++){ this.p[i] = i; this.s[i] = 1; } } public int leader(int a){ Vector<Integer> visit = new Vector<Integer>(); while (this.p[a] != a){ visit.addElement(a); a = this.p[a]; } for (Integer v: visit){ this.p[v] = a; } return a; } public int merge(int a,int b){ int pa = this.leader(a); int pb = this.leader(b); if (pa == pb){ return pa; }else if (this.s[pa] >= this.s[pb]){ this.s[pa] += this.s[pb]; this.p[pb] = pa; return pa; }else{ this.s[pb] += this.s[pa]; this.p[pa] = pb; return pb; } } public Boolean same(int a, int b){ int pa = this.leader(a); int pb = this.leader(b); return (pa == pb); } public int size(int a){ int pa = this.leader(a); return this.s[pa]; } }