結果

問題 No.317 辺の追加
ユーザー uafr_csuafr_cs
提出日時 2015-12-10 04:07:38
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,889 bytes
コンパイル時間 2,403 ms
コンパイル使用メモリ 76,856 KB
実行使用メモリ 62,452 KB
最終ジャッジ日時 2023-10-13 10:18:17
合計ジャッジ時間 8,729 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
54,136 KB
testcase_01 AC 44 ms
49,636 KB
testcase_02 AC 1,488 ms
61,544 KB
testcase_03 AC 768 ms
61,472 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.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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;
import java.util.StringTokenizer;

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]);
		}
	}
	
	public static class Scanner implements Closeable {
		private BufferedReader br;
		private StringTokenizer tok;

		public Scanner(InputStream is) {
			br = new BufferedReader(new InputStreamReader(is));
		}

		private void getLine() {
			try {
				while (!hasNext()) {
					tok = new StringTokenizer(br.readLine());
				}
			} catch (IOException e) { /* ignore */
			}
		}

		private boolean hasNext() {
			return tok != null && tok.hasMoreTokens();
		}

		public String next() {
			getLine();
			return tok.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public void close() {
			try {
				br.close();
			} catch (IOException e) { /* ignore */
			}
		}
	}
}
0