結果

問題 No.317 辺の追加
ユーザー te-shte-sh
提出日時 2017-06-21 11:34:59
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,733 bytes
コンパイル時間 882 ms
コンパイル使用メモリ 103,532 KB
実行使用メモリ 15,052 KB
最終ジャッジ日時 2023-09-03 14:40:10
合計ジャッジ時間 14,405 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,756 KB
testcase_01 AC 2 ms
4,376 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 116 ms
12,700 KB
testcase_22 AC 66 ms
7,052 KB
testcase_23 WA -
testcase_24 AC 173 ms
14,300 KB
testcase_25 AC 140 ms
11,644 KB
testcase_26 AC 153 ms
13,784 KB
testcase_27 AC 132 ms
12,936 KB
testcase_28 WA -
testcase_29 AC 14 ms
4,376 KB
testcase_30 TLE -
testcase_31 TLE -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap

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];
    auto dp2 = dp.dup;
    foreach (k; 0..n+1)
      dp[k] -= k / u;

    auto deqs = new DList!(size_t)*[](u);
    foreach (i; 0..u) deqs[i] = new DList!size_t();

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

  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