結果

問題 No.19 ステージの選択
ユーザー SSRSSSRS
提出日時 2020-11-08 22:56:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,002 bytes
コンパイル時間 1,702 ms
コンパイル使用メモリ 176,936 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-29 21:45:09
合計ジャッジ時間 2,911 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
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 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 100000;
struct strongly_connected_components{
  int cnt;
  vector<int> scc;
  void dfs(vector<vector<int>> &E, vector<bool> &used, vector<int> &ord, int v){
    used[v] = true;
    for (int w : E[v]){
      if (!used[w]){
        dfs(E, used, ord, w);
      }
    }
    ord.push_back(v);
  }
  strongly_connected_components(vector<vector<int>> &E){
    int N = E.size();
    vector<vector<int>> E2(N);
    for (int i = 0; i < N; i++){
      for (int j : E[i]){
        E2[j].push_back(i);
      }
    }
    vector<int> ord;
    vector<bool> used1(N, false);
    for (int i = 0; i < N; i++){
      if (!used1[i]){
        dfs(E2, used1, ord, i);
      }
    }
    reverse(ord.begin(), ord.end());
    cnt = 0;
    scc = vector<int>(N, -1);
    for (int i = 0; i < N; i++){
      if (scc[ord[i]] == -1){
        scc[ord[i]] = cnt;
        queue<int> Q;
        Q.push(ord[i]);
        while (!Q.empty()){
          int v = Q.front();
          Q.pop();
          for (int w : E[v]){
            if (scc[w] == -1){
              scc[w] = cnt;
              Q.push(w);
            }
          }
        }
        cnt++;
      }
    }
  }
  int size(){
    return cnt;
  }
  int operator [](int k){
    return scc[k];
  }
};
int main(){
  cout << fixed << setprecision(20);
  int N;
  cin >> N;
  vector<int> L(N), S(N);
  for (int i = 0; i < N; i++){
    cin >> L[i] >> S[i];
    S[i]--;
  }
  vector<vector<int>> E(N);
  for (int i = 0; i < N; i++){
    E[S[i]].push_back(i);
  }
  strongly_connected_components G(E);
  int M = G.size();
  vector<int> d(M, 0);
  for (int i = 0; i < N; i++){
    if (G[S[i]] != G[i]){
      d[G[i]]++;
    }
  }
  vector<int> mn(M, INF);
  for (int i = 0; i < N; i++){
    mn[G[i]] = min(mn[G[i]], L[i]);
  }
  double ans = 0;
  for (int i = 0; i < N; i++){
    ans += (double) L[i] / 2;
  }
  for (int i = 0; i < M; i++){
    if (d[i] == 0){
      ans += (double) mn[i] / 2;
    }
  }
  cout << ans << endl;
}
0