結果

問題 No.317 辺の追加
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-18 17:30:35
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 2,565 bytes
コンパイル時間 4,825 ms
コンパイル使用メモリ 113,308 KB
実行使用メモリ 6,812 KB
最終ジャッジ日時 2023-09-03 22:20:47
合計ジャッジ時間 12,414 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 88 ms
4,996 KB
testcase_03 AC 106 ms
5,708 KB
testcase_04 AC 86 ms
5,912 KB
testcase_05 AC 110 ms
5,744 KB
testcase_06 AC 133 ms
6,520 KB
testcase_07 AC 46 ms
4,420 KB
testcase_08 AC 111 ms
6,200 KB
testcase_09 AC 110 ms
6,516 KB
testcase_10 AC 148 ms
6,760 KB
testcase_11 AC 51 ms
5,692 KB
testcase_12 AC 148 ms
6,228 KB
testcase_13 AC 64 ms
5,916 KB
testcase_14 AC 125 ms
5,992 KB
testcase_15 AC 141 ms
6,012 KB
testcase_16 AC 98 ms
5,208 KB
testcase_17 AC 130 ms
6,300 KB
testcase_18 AC 148 ms
6,740 KB
testcase_19 AC 150 ms
5,928 KB
testcase_20 AC 99 ms
6,216 KB
testcase_21 AC 47 ms
4,480 KB
testcase_22 AC 26 ms
4,376 KB
testcase_23 AC 27 ms
4,380 KB
testcase_24 AC 80 ms
5,000 KB
testcase_25 AC 58 ms
4,740 KB
testcase_26 AC 61 ms
4,676 KB
testcase_27 AC 49 ms
4,484 KB
testcase_28 AC 28 ms
4,380 KB
testcase_29 AC 8 ms
4,376 KB
testcase_30 AC 154 ms
5,740 KB
testcase_31 AC 150 ms
6,000 KB
testcase_32 AC 151 ms
5,252 KB
testcase_33 AC 151 ms
6,720 KB
testcase_34 AC 148 ms
5,732 KB
testcase_35 AC 152 ms
5,184 KB
testcase_36 AC 152 ms
6,812 KB
testcase_37 AC 152 ms
5,280 KB
testcase_38 AC 153 ms
5,948 KB
testcase_39 AC 150 ms
5,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken().to!int; }
long readLong() { return readToken().to!long; }
real readReal() { return readToken().to!real; }

void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }


int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

int N, M;
int[] A, B;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      M = readInt();
      A = new int[M];
      B = new int[M];
      foreach (i; 0 .. M) {
        A[i] = readInt() - 1;
        B[i] = readInt() - 1;
      }
      
      auto uf = new int[N];
      uf[] = -1;
      foreach (i; 0 .. M) {
        uf.connect(A[i], B[i]);
      }
      
      int[] xs;
      foreach (u; 0 .. N) {
        if (uf[u] < 0) {
          xs ~= -uf[u];
        }
      }
      xs.sort;
      debug {
        writeln("xs = ", xs);
      }
      
      auto dp = new int[N + 1];
      dp[] = N + 1;
      dp[0] = 0;
      for (int i = 0, j = 0; i < xs.length; i = j) {
        for (; j < xs.length && xs[i] == xs[j]; ++j) {}
        int k = j - i;
        for (int l = 1; l < k; l <<= 1) {
          k -= l;
          const y = xs[i] * l;
          foreach_reverse (z; y .. N + 1) {
            chmin(dp[z], dp[z - y] + l);
          }
        }
        {
          const y = xs[i] * k;
          foreach_reverse (z; y .. N + 1) {
            chmin(dp[z], dp[z - y] + k);
          }
        }
      }
      
      foreach (z; 1 .. N + 1) {
        writeln((dp[z] > N) ? -1 : (dp[z] - 1));
      }
    }
  } catch (EOFException e) {
  }
}
0