結果
問題 | No.317 辺の追加 |
ユーザー | uafr_cs |
提出日時 | 2015-12-10 04:18:59 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,517 bytes |
コンパイル時間 | 2,458 ms |
コンパイル使用メモリ | 80,404 KB |
実行使用メモリ | 60,904 KB |
最終ジャッジ日時 | 2024-09-15 07:24:49 |
合計ジャッジ時間 | 31,758 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 53 ms
37,116 KB |
testcase_01 | AC | 52 ms
36,744 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 | 534 ms
50,620 KB |
testcase_22 | AC | 384 ms
46,148 KB |
testcase_23 | WA | - |
testcase_24 | AC | 734 ms
55,344 KB |
testcase_25 | AC | 595 ms
52,172 KB |
testcase_26 | AC | 614 ms
54,564 KB |
testcase_27 | AC | 536 ms
50,588 KB |
testcase_28 | WA | - |
testcase_29 | AC | 231 ms
42,008 KB |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | AC | 839 ms
51,108 KB |
testcase_33 | AC | 861 ms
51,984 KB |
testcase_34 | WA | - |
testcase_35 | AC | 816 ms
51,316 KB |
testcase_36 | AC | 850 ms
51,704 KB |
testcase_37 | AC | 832 ms
51,156 KB |
testcase_38 | WA | - |
testcase_39 | AC | 859 ms
51,348 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; } final int count = entry.getValue() - 1; for(int size_2 = 1; size_2 <= count; size_2 *= 2){ final int size = entry.getKey() * (count % 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); } } } } 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 */ } } } }