結果

問題 No.317 辺の追加
ユーザー GrenacheGrenache
提出日時 2015-12-11 20:02:00
言語 Java19
(openjdk 21)
結果
AC  
実行時間 920 ms / 2,000 ms
コード長 3,489 bytes
コンパイル時間 3,526 ms
コンパイル使用メモリ 77,844 KB
実行使用メモリ 77,396 KB
最終ジャッジ日時 2023-10-13 11:22:55
合計ジャッジ時間 30,022 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,272 KB
testcase_01 AC 45 ms
49,404 KB
testcase_02 AC 700 ms
71,200 KB
testcase_03 AC 720 ms
72,468 KB
testcase_04 AC 650 ms
68,120 KB
testcase_05 AC 676 ms
67,008 KB
testcase_06 AC 817 ms
75,420 KB
testcase_07 AC 331 ms
60,508 KB
testcase_08 AC 835 ms
67,068 KB
testcase_09 AC 713 ms
74,156 KB
testcase_10 AC 894 ms
76,892 KB
testcase_11 AC 411 ms
63,296 KB
testcase_12 AC 920 ms
77,112 KB
testcase_13 AC 517 ms
64,104 KB
testcase_14 AC 841 ms
77,124 KB
testcase_15 AC 892 ms
77,396 KB
testcase_16 AC 735 ms
72,864 KB
testcase_17 AC 886 ms
77,272 KB
testcase_18 AC 907 ms
77,336 KB
testcase_19 AC 879 ms
76,912 KB
testcase_20 AC 714 ms
71,372 KB
testcase_21 AC 319 ms
62,080 KB
testcase_22 AC 242 ms
60,040 KB
testcase_23 AC 239 ms
60,840 KB
testcase_24 AC 430 ms
71,896 KB
testcase_25 AC 352 ms
64,752 KB
testcase_26 AC 358 ms
64,296 KB
testcase_27 AC 308 ms
64,968 KB
testcase_28 AC 241 ms
60,228 KB
testcase_29 AC 164 ms
53,936 KB
testcase_30 AC 709 ms
74,352 KB
testcase_31 AC 697 ms
74,632 KB
testcase_32 AC 715 ms
74,160 KB
testcase_33 AC 712 ms
74,280 KB
testcase_34 AC 714 ms
72,372 KB
testcase_35 AC 765 ms
74,444 KB
testcase_36 AC 714 ms
74,520 KB
testcase_37 AC 720 ms
74,280 KB
testcase_38 AC 738 ms
74,556 KB
testcase_39 AC 705 ms
74,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


public class Main_yukicoder317_2 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        int n = sc.nextInt();
        int m = sc.nextInt();
        List<List<Integer>> edges = new ArrayList<List<Integer>>();
        for (int i = 0; i < n; i++) {
        	edges.add(new ArrayList<Integer>());
        }
        for (int i = 0; i < m; i++) {
        	int u = sc.nextInt() - 1;
        	int v = sc.nextInt() - 1;
        	edges.get(u).add(v);
        	edges.get(v).add(u);
        }

        boolean[] used = new boolean[n];
        Map<Integer, Integer> cnt = new HashMap<Integer, Integer>();
        for (int i = 0; i < n; i++) {
        	if (used[i]) {
        		continue;
        	}

        	Deque<Integer> st = new ArrayDeque<Integer>();
        	st.push(i);
        	used[i] = true;

        	int tmp = 1;
        	while (!st.isEmpty()) {
        		int v = st.pop();

        		for (int e : edges.get(v)) {
        			if (used[e]) {
        				continue;
        			}

        			st.push(e);
        			used[e] = true;
        			tmp++;
        		}
        	}

        	if (cnt.containsKey(tmp)) {
        		cnt.put(tmp, cnt.get(tmp) + 1);
        	} else {
        		cnt.put(tmp, 1);
        	}
        }

        int[] dp = new int[n + 1];
        final int INF = Integer.MAX_VALUE / 2;
        Arrays.fill(dp, INF);
        dp[0] = 0;

        for (Entry<Integer, Integer> v : cnt.entrySet()) {
        	int w = v.getKey();
        	int nn = v.getValue();
        	for (int j = 1; nn > 0; j *= 2) {
        		int tmp = Math.min(j, nn);
        		for (int i = n; i >= 0; i--) {
        			if (i - tmp * w >= 0) {
        				dp[i] = Math.min(dp[i], dp[i - tmp * w] + tmp);
        			}
        		}

    			nn -= j;
        	}
        }

        for (int i = 1; i <= n; i++) {
        	if (dp[i] == INF) {
        		pr.println(-1);
        	} else {
        		pr.println(dp[i] - 1);
        	}
        }

        pr.close();
        sc.close();
    }

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

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

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

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

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

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0