結果

問題 No.748 yuki国のお財布事情
ユーザー te-shte-sh
提出日時 2018-11-05 15:47:52
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 1,951 bytes
コンパイル時間 703 ms
コンパイル使用メモリ 100,336 KB
実行使用メモリ 10,092 KB
最終ジャッジ日時 2023-09-03 21:14:54
合計ジャッジ時間 2,920 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 10 ms
4,376 KB
testcase_14 AC 19 ms
4,684 KB
testcase_15 AC 13 ms
4,380 KB
testcase_16 AC 35 ms
6,228 KB
testcase_17 AC 64 ms
9,840 KB
testcase_18 AC 79 ms
9,564 KB
testcase_19 AC 90 ms
9,100 KB
testcase_20 AC 81 ms
10,092 KB
testcase_21 AC 73 ms
9,312 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 57 ms
9,040 KB
testcase_26 AC 68 ms
9,624 KB
testcase_27 AC 67 ms
8,784 KB
testcase_28 AC 63 ms
7,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}

void main()
{
  int n, m, k; readV(n, m, k);
  int[] a, b, c; readC(m, a, b, c); --a[]; --b[];
  int[] e; readC(k, e); --e[];

  struct R { int a, b, c, i; }
  auto r = new R[](m);
  foreach (i; 0..m) r[i] = R(a[i], b[i], c[i], i);

  auto v = new bool[](m), uf = new UnionFind!int(n);

  foreach (ei; e) {
    uf.unite(r[ei].a, r[ei].b);
    v[ei] = true;
  }

  auto r2 = r.dup.sort!"a.c<b.c";
  foreach (ri; r2)
    if (!v[ri.i] && !uf.isSame(ri.a, ri.b)) {
      uf.unite(ri.a, ri.b);
      v[ri.i] = true;
    }

  auto s = 0L;
  foreach (i; 0..m)
    if (!v[i]) s += r[i].c;

  writeln(s);
}

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

  T[] p; // parent
  const T s; // sentinel
  const T n;
  T countForests; // number of forests
  T[] countNodes; // number of nodes in forests

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

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

  bool unite(T i, T j)
  {
    auto pi = this[i], pj = this[j];
    if (pi != pj) {
      p[pj] = pi;
      --countForests;
      countNodes[pi] += countNodes[pj];
      return true;
    } else {
      return false;
    }
  }

  auto countNodesOf(T i) { return countNodes[this[i]]; }
  bool isSame(T i, T j) { return this[i] == this[j]; }

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