結果
問題 | No.317 辺の追加 |
ユーザー | Grenache |
提出日時 | 2015-12-12 12:49:24 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,047 ms / 2,000 ms |
コード長 | 3,805 bytes |
コンパイル時間 | 3,878 ms |
コンパイル使用メモリ | 81,484 KB |
実行使用メモリ | 66,368 KB |
最終ジャッジ日時 | 2024-09-15 08:42:01 |
合計ジャッジ時間 | 34,289 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge6 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
36,752 KB |
testcase_01 | AC | 51 ms
36,924 KB |
testcase_02 | AC | 681 ms
59,384 KB |
testcase_03 | AC | 761 ms
62,264 KB |
testcase_04 | AC | 640 ms
58,772 KB |
testcase_05 | AC | 750 ms
56,408 KB |
testcase_06 | AC | 930 ms
65,248 KB |
testcase_07 | AC | 365 ms
50,408 KB |
testcase_08 | AC | 796 ms
57,096 KB |
testcase_09 | AC | 845 ms
64,844 KB |
testcase_10 | AC | 948 ms
66,192 KB |
testcase_11 | AC | 407 ms
52,048 KB |
testcase_12 | AC | 947 ms
66,208 KB |
testcase_13 | AC | 471 ms
55,764 KB |
testcase_14 | AC | 894 ms
65,716 KB |
testcase_15 | AC | 929 ms
65,148 KB |
testcase_16 | AC | 768 ms
62,444 KB |
testcase_17 | AC | 878 ms
66,144 KB |
testcase_18 | AC | 947 ms
66,368 KB |
testcase_19 | AC | 1,011 ms
65,764 KB |
testcase_20 | AC | 759 ms
60,724 KB |
testcase_21 | AC | 350 ms
52,404 KB |
testcase_22 | AC | 263 ms
48,720 KB |
testcase_23 | AC | 259 ms
48,332 KB |
testcase_24 | AC | 487 ms
59,772 KB |
testcase_25 | AC | 387 ms
56,964 KB |
testcase_26 | AC | 408 ms
57,616 KB |
testcase_27 | AC | 368 ms
53,056 KB |
testcase_28 | AC | 273 ms
49,068 KB |
testcase_29 | AC | 193 ms
44,572 KB |
testcase_30 | AC | 945 ms
62,528 KB |
testcase_31 | AC | 972 ms
62,524 KB |
testcase_32 | AC | 1,010 ms
62,856 KB |
testcase_33 | AC | 961 ms
62,832 KB |
testcase_34 | AC | 947 ms
62,376 KB |
testcase_35 | AC | 1,047 ms
62,784 KB |
testcase_36 | AC | 984 ms
62,672 KB |
testcase_37 | AC | 958 ms
62,648 KB |
testcase_38 | AC | 954 ms
62,400 KB |
testcase_39 | AC | 959 ms
62,336 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_4 { 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; // Deque<Integer> qi = new ArrayDeque<Integer>(); // Deque<Integer> qdp = new ArrayDeque<Integer>(); int[] qi = new int[n + 1]; int[] qdp = new int[n + 1]; for (Entry<Integer, Integer> v : cnt.entrySet()) { int w = v.getKey(); int nn = v.getValue(); for (int j = 0; j < w; j++) { int s = 0; int t = 0; for (int i = 0; i * w + j <= n; i++) { int tmp = dp[i * w + j] - i; while (s < t && qdp[t - 1] >= tmp) { t--; } qi[t] = i; qdp[t++] = tmp; dp[i * w + j] = qdp[s] + i; if (qi[s] == i - nn) { s++; } } } } 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); } } }