結果
問題 | No.317 辺の追加 |
ユーザー | uafr_cs |
提出日時 | 2015-12-10 04:28:51 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,907 bytes |
コンパイル時間 | 2,631 ms |
コンパイル使用メモリ | 80,196 KB |
実行使用メモリ | 59,124 KB |
最終ジャッジ日時 | 2024-09-15 07:25:39 |
合計ジャッジ時間 | 32,928 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
36,692 KB |
testcase_01 | AC | 53 ms
36,612 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 | 533 ms
48,468 KB |
testcase_22 | AC | 381 ms
46,408 KB |
testcase_23 | WA | - |
testcase_24 | AC | 752 ms
54,360 KB |
testcase_25 | AC | 614 ms
52,092 KB |
testcase_26 | AC | 626 ms
54,856 KB |
testcase_27 | AC | 543 ms
48,200 KB |
testcase_28 | AC | 399 ms
46,328 KB |
testcase_29 | AC | 225 ms
41,744 KB |
testcase_30 | WA | - |
testcase_31 | AC | 865 ms
51,792 KB |
testcase_32 | AC | 849 ms
52,084 KB |
testcase_33 | AC | 840 ms
48,888 KB |
testcase_34 | AC | 892 ms
51,896 KB |
testcase_35 | AC | 854 ms
51,140 KB |
testcase_36 | AC | 886 ms
51,204 KB |
testcase_37 | AC | 858 ms
51,388 KB |
testcase_38 | WA | - |
testcase_39 | AC | 854 ms
51,276 KB |
ソースコード
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.Map.Entry; import java.util.Scanner; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; 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>(); TreeMap<Integer, Integer> counts = new TreeMap<Integer, Integer>(); for(int i = 0; i < N; i++){ if(already.contains(uf.find(i))){ continue; } already.add(uf.find(i)); if(!counts.containsKey(uf.size(i))){ counts.put(uf.size(i), 0); } counts.put(uf.size(i), counts.get(uf.size(i)) + 1); } int[] DP = new int[N + 1]; Arrays.fill(DP, -1); DP[0] = 0; for(final Entry<Integer, Integer> entry : counts.entrySet()){ { final int size = entry.getKey(); //System.out.println(size); 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; } int count = entry.getValue() - 1; for(int size_2 = 1; size_2 <= count; count -= size_2, size_2 *= 2){ final int size = entry.getKey() * size_2; //System.out.println(":" + size_2); 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); } } } { final int size = count * entry.getKey(); if(size == 0){ continue; } //System.out.println("::" + size); 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); } } } } 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 */ } } } }