結果
問題 | No.556 仁義なきサルたち |
ユーザー |
|
提出日時 | 2017-08-12 01:11:42 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 445 ms / 2,000 ms |
コード長 | 1,187 bytes |
コンパイル時間 | 2,856 ms |
コンパイル使用メモリ | 77,132 KB |
実行使用メモリ | 48,280 KB |
最終ジャッジ日時 | 2024-10-12 22:46:53 |
合計ジャッジ時間 | 9,392 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
import java.util.Arrays; import java.util.Scanner; public class Main { final long MODULO = 1_000_000_000 + 7; void run() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); DJSet ds = new DJSet(n); for (int i = 0; i < m; ++i) { int a = sc.nextInt(); int b = sc.nextInt(); --a; --b; ds.setUnion(a, b); } for (int i = 0; i < n; ++i) { System.out.println(ds.root(i) + 1); } } class DJSet { int n; int[] upper; public DJSet(int n_) { n = n_; upper = new int[n]; Arrays.fill(upper, -1); } boolean equiv(int x, int y) { return root(x) == root(y); } int root(int x) { return upper[x] < 0 ? x : (upper[x] = root(upper[x])); } void setUnion(int x, int y) { x = root(x); y = root(y); if (x == y) { return; } if (upper[x] < upper[y]) { int d = x; x = y; y = d; } if (upper[x] == upper[y] && x < y) { int d = x; x = y; y = d; } upper[y] += upper[x]; upper[x] = y; } } static void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } public static void main(String[] args) { new Main().run(); } }