結果
問題 | No.556 仁義なきサルたち |
ユーザー | tenten |
提出日時 | 2020-09-07 23:24:42 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 302 ms / 2,000 ms |
コード長 | 1,386 bytes |
コンパイル時間 | 2,298 ms |
コンパイル使用メモリ | 77,528 KB |
実行使用メモリ | 47,596 KB |
最終ジャッジ日時 | 2024-05-07 00:20:00 |
合計ジャッジ時間 | 6,893 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 104 ms
40,592 KB |
testcase_01 | AC | 103 ms
39,988 KB |
testcase_02 | AC | 101 ms
39,720 KB |
testcase_03 | AC | 110 ms
40,864 KB |
testcase_04 | AC | 101 ms
39,980 KB |
testcase_05 | AC | 117 ms
41,008 KB |
testcase_06 | AC | 121 ms
41,268 KB |
testcase_07 | AC | 133 ms
41,288 KB |
testcase_08 | AC | 146 ms
41,248 KB |
testcase_09 | AC | 154 ms
41,624 KB |
testcase_10 | AC | 168 ms
42,796 KB |
testcase_11 | AC | 174 ms
43,540 KB |
testcase_12 | AC | 178 ms
43,288 KB |
testcase_13 | AC | 172 ms
42,132 KB |
testcase_14 | AC | 185 ms
45,644 KB |
testcase_15 | AC | 222 ms
45,792 KB |
testcase_16 | AC | 210 ms
45,832 KB |
testcase_17 | AC | 235 ms
47,088 KB |
testcase_18 | AC | 280 ms
47,452 KB |
testcase_19 | AC | 279 ms
46,176 KB |
testcase_20 | AC | 284 ms
47,332 KB |
testcase_21 | AC | 302 ms
47,596 KB |
ソースコード
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(); UnionFindTree uft = new UnionFindTree(n); for (int i = 0; i < m; i++) { uft.unite(sc.nextInt() - 1, sc.nextInt() - 1); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(uft.find(i) + 1).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 (x == parents[x]) { return x; } else { return parents[x] = find(parents[x]); } } public void unite(int x, int y) { int xx = find(x); int yy = find(y); if (xx == yy) { return; } if ((counts[xx] == counts[yy] && xx < yy) || counts[xx] > counts[yy]) { counts[xx] += counts[yy]; parents[yy] = xx; } else { counts[yy] += counts[xx]; parents[xx] = yy; } } } }