結果

問題 No.317 辺の追加
ユーザー te-shte-sh
提出日時 2017-06-21 13:45:11
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 323 ms / 2,000 ms
コード長 2,006 bytes
コンパイル時間 1,048 ms
コンパイル使用メモリ 103,864 KB
実行使用メモリ 25,052 KB
最終ジャッジ日時 2023-09-03 14:40:51
合計ジャッジ時間 9,677 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 147 ms
8,068 KB
testcase_03 AC 151 ms
6,532 KB
testcase_04 AC 195 ms
15,036 KB
testcase_05 AC 151 ms
4,696 KB
testcase_06 AC 187 ms
5,532 KB
testcase_07 AC 60 ms
4,380 KB
testcase_08 AC 323 ms
25,052 KB
testcase_09 AC 188 ms
15,556 KB
testcase_10 AC 225 ms
7,600 KB
testcase_11 AC 97 ms
14,988 KB
testcase_12 AC 222 ms
6,968 KB
testcase_13 AC 146 ms
16,092 KB
testcase_14 AC 197 ms
14,512 KB
testcase_15 AC 216 ms
11,612 KB
testcase_16 AC 213 ms
18,720 KB
testcase_17 AC 202 ms
9,916 KB
testcase_18 AC 214 ms
7,056 KB
testcase_19 AC 221 ms
6,464 KB
testcase_20 AC 237 ms
22,820 KB
testcase_21 AC 58 ms
6,552 KB
testcase_22 AC 32 ms
5,224 KB
testcase_23 AC 34 ms
7,300 KB
testcase_24 AC 97 ms
7,596 KB
testcase_25 AC 70 ms
7,056 KB
testcase_26 AC 74 ms
7,276 KB
testcase_27 AC 60 ms
7,312 KB
testcase_28 AC 33 ms
6,532 KB
testcase_29 AC 9 ms
4,380 KB
testcase_30 AC 235 ms
7,588 KB
testcase_31 AC 222 ms
7,580 KB
testcase_32 AC 222 ms
7,584 KB
testcase_33 AC 224 ms
7,532 KB
testcase_34 AC 220 ms
7,520 KB
testcase_35 AC 223 ms
7,536 KB
testcase_36 AC 221 ms
7,532 KB
testcase_37 AC 224 ms
7,520 KB
testcase_38 AC 233 ms
7,584 KB
testcase_39 AC 223 ms
7,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap
import std.typecons;  // Tuple, Nullable, BigFlags

const inf = 10 ^^ 6;

void main()
{
  auto rd1 = readln.split.to!(size_t[]), n = rd1[0], m = rd1[1];
  auto uf = UnionFind!size_t(n);

  foreach (_; 0..m) {
    auto rd2 = readln.split.to!(size_t[]), x = rd2[0]-1, y = rd2[1]-1;
    uf.unite(x, y);
  }

  auto si = new int[](n);
  foreach (i; 0..n) ++si[uf.find(i)];
  auto ti = si.filter!"a > 0".array.sort().group.array;

  auto dp = new int[](n+1), ma = 0;
  dp[1..$] = inf;

  foreach (t; ti) {
    auto u = t[0], v = t[1].to!int;
    auto dp2 = new int[](n+1);

    if (v <= 20) {
      foreach (k; 0..n+1) {
        dp2[k] = dp[k];
        foreach (j; 1..v+1) {
          if (k < u*j) break;
          dp2[k] = min(dp2[k], dp[k-u*j] + j);
        }
      }
    } else {
      foreach (k; 0..n.to!int+1)
        if (dp[k] < inf)
          dp[k] -= k/u;

      auto deqs = new DList!size_t[](u);

      foreach (k; 0..n.to!int+1) {
        auto deq = &deqs[k % u], mi = max(k - u*v, 0);
        while (!deq.empty && deq.front < mi) deq.removeFront();
        while (!deq.empty && dp[deq.back] >= dp[k]) deq.removeBack();
        deq.insertBack(k);
        dp2[k] = dp[deq.front] + k/u;
      }
    }
    dp = dp2;
  }

  foreach (r; dp[1..$]) writeln(r >= inf ? -1 : r-1);
}

struct UnionFind(T)
{
  import std.algorithm, std.range;

  T[] p; // parent
  const T s; // sentinel
  const T n;

  this(T n)
  {
    this.n = n;
    p = new T[](n);
    s = n + 1;
    p[] = s;
  }

  T find(T i)
  {
    if (p[i] == s) {
      return i;
    } else {
      p[i] = find(p[i]);
      return p[i];
    }
  }

  void unite(T i, T j)
  {
    auto pi = find(i), pj = find(j);
    if (pi != pj) p[pj] = pi;
  }

  bool isSame(T i, T j) { return find(i) == find(j); }

  auto groups()
  {
    auto g = new T[][](n);
    foreach (i; 0..n) g[find(i)] ~= i;
    return g.filter!(l => !l.empty);
  }
}
0