結果

問題 No.556 仁義なきサルたち
ユーザー 37zigen37zigen
提出日時 2017-08-12 01:11:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 432 ms / 2,000 ms
コード長 1,187 bytes
コンパイル時間 2,038 ms
コンパイル使用メモリ 78,068 KB
実行使用メモリ 48,448 KB
最終ジャッジ日時 2024-04-21 00:26:59
合計ジャッジ時間 8,337 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
40,860 KB
testcase_01 AC 129 ms
41,004 KB
testcase_02 AC 120 ms
41,436 KB
testcase_03 AC 122 ms
40,884 KB
testcase_04 AC 121 ms
40,252 KB
testcase_05 AC 148 ms
41,788 KB
testcase_06 AC 147 ms
41,596 KB
testcase_07 AC 164 ms
42,040 KB
testcase_08 AC 168 ms
41,832 KB
testcase_09 AC 198 ms
42,428 KB
testcase_10 AC 216 ms
43,268 KB
testcase_11 AC 214 ms
43,804 KB
testcase_12 AC 227 ms
43,552 KB
testcase_13 AC 256 ms
43,508 KB
testcase_14 AC 304 ms
47,028 KB
testcase_15 AC 307 ms
47,160 KB
testcase_16 AC 329 ms
47,468 KB
testcase_17 AC 360 ms
48,420 KB
testcase_18 AC 429 ms
48,448 KB
testcase_19 AC 379 ms
46,744 KB
testcase_20 AC 392 ms
46,924 KB
testcase_21 AC 432 ms
47,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
	}
}
0