結果
問題 | No.317 辺の追加 |
ユーザー | Grenache |
提出日時 | 2015-12-12 11:22:43 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,857 bytes |
コンパイル時間 | 3,870 ms |
コンパイル使用メモリ | 80,288 KB |
実行使用メモリ | 68,460 KB |
最終ジャッジ日時 | 2024-09-15 08:39:23 |
合計ジャッジ時間 | 43,465 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 50 ms
37,052 KB |
testcase_01 | AC | 51 ms
36,732 KB |
testcase_02 | AC | 804 ms
63,568 KB |
testcase_03 | AC | 807 ms
63,980 KB |
testcase_04 | AC | 942 ms
61,392 KB |
testcase_05 | AC | 765 ms
60,396 KB |
testcase_06 | AC | 945 ms
65,620 KB |
testcase_07 | AC | 364 ms
50,556 KB |
testcase_08 | WA | - |
testcase_09 | AC | 964 ms
64,144 KB |
testcase_10 | AC | 1,053 ms
66,804 KB |
testcase_11 | AC | 575 ms
52,208 KB |
testcase_12 | AC | 1,026 ms
66,460 KB |
testcase_13 | AC | 583 ms
53,948 KB |
testcase_14 | AC | 991 ms
64,640 KB |
testcase_15 | AC | 1,153 ms
68,460 KB |
testcase_16 | AC | 837 ms
62,456 KB |
testcase_17 | AC | 1,011 ms
65,328 KB |
testcase_18 | AC | 1,046 ms
66,544 KB |
testcase_19 | AC | 1,070 ms
66,600 KB |
testcase_20 | AC | 926 ms
61,920 KB |
testcase_21 | AC | 431 ms
52,764 KB |
testcase_22 | AC | 346 ms
52,672 KB |
testcase_23 | AC | 356 ms
52,976 KB |
testcase_24 | AC | 683 ms
64,820 KB |
testcase_25 | AC | 574 ms
57,632 KB |
testcase_26 | AC | 578 ms
58,520 KB |
testcase_27 | AC | 461 ms
52,808 KB |
testcase_28 | AC | 377 ms
52,656 KB |
testcase_29 | AC | 199 ms
45,696 KB |
testcase_30 | WA | - |
testcase_31 | AC | 1,512 ms
64,608 KB |
testcase_32 | AC | 1,545 ms
62,940 KB |
testcase_33 | AC | 1,657 ms
66,104 KB |
testcase_34 | AC | 1,618 ms
66,360 KB |
testcase_35 | AC | 1,519 ms
63,704 KB |
testcase_36 | AC | 1,661 ms
66,044 KB |
testcase_37 | AC | 1,658 ms
66,044 KB |
testcase_38 | WA | - |
testcase_39 | AC | 1,534 ms
63,512 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_3 { 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 = 0; j < w; j++) { Deque<Integer> qi = new ArrayDeque<Integer>(); Deque<Integer> qdp = new ArrayDeque<Integer>(); for (int i = 0; i * w + j <= n; i++) { int tmp = dp[i * w + j]; while (!qi.isEmpty() && qdp.peekLast() >= dp[i * w + j]) { qi.removeLast(); qdp.removeLast(); } qi.add(i); qdp.addLast(tmp); dp[i * w + j] = qdp.peekFirst() + i - qi.peekFirst(); if (qi.peekFirst() == i - nn) { qi.removeFirst(); qdp.removeFirst(); } } } } 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); } } }