結果

問題 No.330 Eigenvalue Decomposition
ユーザー te-shte-sh
提出日時 2017-07-19 17:57:13
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 203 ms / 5,000 ms
コード長 1,005 bytes
コンパイル時間 655 ms
コンパイル使用メモリ 87,288 KB
実行使用メモリ 4,420 KB
最終ジャッジ日時 2023-09-03 15:18:54
合計ジャッジ時間 4,688 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 84 ms
4,412 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 90 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 60 ms
4,380 KB
testcase_09 AC 165 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 54 ms
4,380 KB
testcase_12 AC 180 ms
4,412 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 49 ms
4,376 KB
testcase_15 AC 7 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 6 ms
4,380 KB
testcase_18 AC 31 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 8 ms
4,380 KB
testcase_21 AC 203 ms
4,420 KB
testcase_22 AC 5 ms
4,380 KB
testcase_23 AC 14 ms
4,380 KB
testcase_24 AC 166 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 8 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,384 KB
testcase_35 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

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

  auto uf = UnionFind!size_t(n);
  foreach (_; 0..m) {
    auto rd2 = readln.split[0..2].to!(size_t[]), i = rd2[0]-1, j = rd2[1]-1;
    uf.unite(i, j);
  }

  auto b = new bool[](n);
  foreach (i; 0..n) b[uf.find(i)] = true;

  writeln(b.count!"a");
}

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