結果

問題 No.19 ステージの選択
ユーザー nebukuro09nebukuro09
提出日時 2016-11-22 14:21:36
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 2,260 bytes
コンパイル時間 1,929 ms
コンパイル使用メモリ 159,864 KB
実行使用メモリ 4,496 KB
最終ジャッジ日時 2023-09-02 23:00:45
合計ジャッジ時間 3,145 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,372 KB
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 AC 1 ms
4,368 KB
testcase_12 AC 1 ms
4,372 KB
testcase_13 AC 2 ms
4,372 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio;
import std.array;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
import std.range;
import std.random;
import std.math;
import std.container;
import std.numeric;


class Kosaraju {
  int N;
  int[][] adj;
  int[][] rev_adj;
  int[] group;
  bool[] group_prev;
  int[][] group_n;

  this(int n) {
    N = n;
    adj = new int[][](N);
    rev_adj = new int[][](N);
    group = new int[](N);
  }

  void add_edge(int from, int to) {
    adj[from] ~= to;
    rev_adj[to] ~= from;
  }

  void run() {
    int[] ord;
    auto used = new bool[](N);
    foreach (i; 0..N)
      if (!used[i])
        dfs1(i, ord, used);
    foreach (i; 0..N) used[i] = false;
    auto g_num = 0;
    foreach (i; iota(N-1, -1, -1))
      if (!used[ord[i]]) {
        dfs2(ord[i], g_num, used);
        g_num += 1;
      }
    group_n = new int[][](group.reduce!(max)+1);
    foreach (i; 0..N)
      group_n[group[i]] ~= i;
    group_prev = new bool[](group.reduce!(max)+1);
    foreach (i; 0..N) used[i] = false;
    foreach (i; 0..N)
      if (!used[i])
        dfs3(i, used);
  }
  
  void dfs1(int from, ref int[] ord, ref bool[] used) {
    if (used[from]) return;
    used[from] = true;
    foreach (to; adj[from]) 
      if (!used[to])
        dfs1(to, ord, used);
    ord ~= from;
  }

  void dfs2(int from, int g_num, ref bool[] used) {
    if (used[from]) return;
    used[from] = true;
    group[from] = g_num;
    foreach (to; rev_adj[from])
      if (!used[to])
        dfs2(to, g_num, used);
  }

  void dfs3(int from, ref bool[] used) {
    if (used[from]) return;
    used[from] = true;
    foreach (to; adj[from])
      if (!used[to]) {
        if (group[from] != group[to])
          group_prev[group[to]] = true;
        dfs3(to, used);
      }
  }
}

void main() {
  auto N = readln.chomp.to!int;
  auto ks = new Kosaraju(N);
  auto L = new int[](N);
  foreach (i; 0..N) {
    auto LS = readln.split.map!(to!int);
    L[i] = LS[0];
    if (LS[1]-1 != i) ks.add_edge(LS[1]-1, i);
  }
  ks.run();
  real ans = 0;
  foreach (g; 0..ks.group_n.length) {
    ans += ks.group_n[g].map!(n => L[n]/2.0).sum;
    if (!ks.group_prev[g])
      ans += ks.group_n[g].map!(n => L[n]/2.0).reduce!(min);
  }
  writefln("%.1f", ans);
}
0