結果

問題 No.317 辺の追加
ユーザー uafr_csuafr_cs
提出日時 2015-12-10 04:06:49
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,946 bytes
コンパイル時間 3,028 ms
コンパイル使用メモリ 77,800 KB
実行使用メモリ 69,700 KB
最終ジャッジ日時 2023-10-13 10:18:08
合計ジャッジ時間 10,687 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
60,180 KB
testcase_01 AC 126 ms
55,420 KB
testcase_02 AC 1,942 ms
65,852 KB
testcase_03 AC 1,243 ms
65,784 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {

	public static class UnionFind {
		int[] par; //

		public UnionFind(int n) {
			par = new int[n];
			for (int i = 0; i < n; i++) {
				par[i] = -1;
			}
		}

		public int find(int x) {
			if (par[x] < 0) {
				return x;
			} else {
				return par[x] = find(par[x]);
			}
		}

		public boolean union(int x, int y) {
			x = find(x);
			y = find(y);

			if (x != y) {
				if (par[y] < par[x]) { // 多い方が根になるようにスワップする.
					int tmp = x;
					x = y;
					y = tmp;
				}
				par[x] += par[y];
				par[y] = x;
				return true;
			} else {
				return false;
			}
		}

		public boolean same(int x, int y) {
			return find(x) == find(y);
		}

		public int size(int x) {
			return -par[find(x)];
		}
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		final int M = sc.nextInt();
		
		UnionFind uf = new UnionFind(N);
		for(int i = 0; i < M; i++){
			final int a = sc.nextInt() - 1;
			final int b = sc.nextInt() - 1;
			uf.union(a, b);
		}
		
		HashSet<Integer> already = new HashSet<Integer>();
		ArrayList<Integer> connected = new ArrayList<Integer>();
		for(int i = 0; i < N; i++){
			if(already.contains(uf.find(i))){ continue; }
			already.add(uf.find(i));
			
			connected.add(uf.size(i));
		}
		
		Collections.sort(connected);
		int[] DP = new int[N + 1];
		Arrays.fill(DP, -1);
		DP[0] = 0;
		
		for(final int size : connected){
			for(int pos = N - size; pos >= 0; pos--){
				if(DP[pos] < 0){ continue; }
				if(DP[pos + size] < 0){ DP[pos + size] = DP[pos] + 1; }
				else{ DP[pos + size] = Math.min(DP[pos + size], DP[pos] + 1); }
			}
			
			DP[size] = 0;
		}
		
		for(int i = 1; i <= N; i++){
			System.out.println(DP[i]);
		}
	}

}
0