結果
問題 | No.317 辺の追加 |
ユーザー | Grenache |
提出日時 | 2015-12-11 19:53:39 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,493 bytes |
コンパイル時間 | 3,768 ms |
コンパイル使用メモリ | 81,924 KB |
実行使用メモリ | 69,592 KB |
最終ジャッジ日時 | 2024-09-15 08:21:19 |
合計ジャッジ時間 | 30,854 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 53 ms
36,948 KB |
testcase_01 | AC | 53 ms
37,156 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | AC | 321 ms
52,680 KB |
testcase_22 | AC | 265 ms
49,428 KB |
testcase_23 | WA | - |
testcase_24 | AC | 485 ms
62,680 KB |
testcase_25 | AC | 382 ms
56,872 KB |
testcase_26 | AC | 394 ms
55,552 KB |
testcase_27 | AC | 330 ms
52,888 KB |
testcase_28 | WA | - |
testcase_29 | AC | 186 ms
44,236 KB |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | AC | 773 ms
62,996 KB |
testcase_33 | AC | 765 ms
62,772 KB |
testcase_34 | WA | - |
testcase_35 | AC | 775 ms
62,720 KB |
testcase_36 | AC | 788 ms
62,632 KB |
testcase_37 | AC | 799 ms
62,544 KB |
testcase_38 | WA | - |
testcase_39 | AC | 829 ms
63,016 KB |
ソースコード
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); } } }