結果

問題 No.19 ステージの選択
ユーザー stoqstoq
提出日時 2021-06-06 11:04:55
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 4,515 bytes
コンパイル時間 4,600 ms
コンパイル使用メモリ 219,028 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-14 17:45:18
合計ジャッジ時間 4,915 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,384 KB
testcase_23 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define MOD_TYPE 2

#pragma region Macros

#include <bits/stdc++.h>
using namespace std;

#if 0
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using Int = boost::multiprecision::cpp_int;
using lld = boost::multiprecision::cpp_dec_float_100;
#endif
#if 1
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;

constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
constexpr int INF = (int)1e9 + 10;
constexpr ll LINF = (ll)4e18;
constexpr ld PI = acos(-1.0);
constexpr ld EPS = 1e-7;
constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)
#define repi(i, n) REPI(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << "\n"
#define Yes(n) cout << ((n) ? "Yes" : "No") << "\n"
#define possible(n) cout << ((n) ? "possible" : "impossible") << "\n"
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n"
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << "\n";

struct io_init
{
  io_init()
  {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(30) << setiosflags(ios::fixed);
  };
} io_init;
template <typename T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}
inline ll CEIL(ll a, ll b)
{
  return (a + b - 1) / b;
}
template <typename A, size_t N, typename T>
inline void Fill(A (&array)[N], const T &val)
{
  fill((T *)array, (T *)(array + N), val);
}
template <typename T, typename U>
constexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept
{
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
constexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept
{
  os << p.first << " " << p.second;
  return os;
}
#pragma endregion

// --------------------------------------

struct SCC
{
  int V;
  vector<vector<int>> G, Gr;
  vector<int> comp, order, used;

  SCC(int V_) : V(V_), G(V_), Gr(V_), comp(V_, -1), used(V_) {}

  void add_edge(int a, int b)
  {
    G[a].emplace_back(b);
    Gr[b].emplace_back(a);
  }

  // 番号はトポロジカル順
  int operator[](int k) { return comp[k]; }

  void dfs(int idx)
  {
    if (used[idx])
      return;
    used[idx] = true;
    for (int to : G[idx])
      dfs(to);
    order.emplace_back(idx);
  }

  void rdfs(int idx, int cnt)
  {
    if (comp[idx] != -1)
      return;
    comp[idx] = cnt;
    for (int to : Gr[idx])
      rdfs(to, cnt);
  }

  // del_mul が false の場合高速になるが多重辺が生える
  vector<vector<int>> build(bool del_mul = true)
  {
    vector<vector<int>> res;
    for (int i = 0; i < G.size(); i++)
      dfs(i);
    reverse(begin(order), end(order));
    int ptr = 0;
    for (int i : order)
      if (comp[i] == -1)
        rdfs(i, ptr), ptr++;

    res.resize(ptr);
    for (int i = 0; i < G.size(); i++)
    {
      for (auto &to : G[i])
      {
        int x = comp[i], y = comp[to];
        if (x == y)
          continue;
        res[x].push_back(y);
      }
    }
    if (del_mul)
    {
      for (auto &&v : res)
      {
        sort(begin(v), end(v));
        auto itr = unique(begin(v), end(v));
        v.erase(itr, v.end());
      }
    }
    return res;
  }
};

void solve()
{
  int n;
  cin >> n;
  SCC scc(n);
  vector<int> l(n);
  rep(i, n)
  {
    int j;
    cin >> l[i] >> j;
    l[i] *= 5;
    j--;
    if (i == j)
      continue;
    scc.add_edge(j, i);
  }
  auto E = scc.build();
  int ns = E.size();
  vector<int> sz(ns, 0);
  vector<int> Min(ns, INF);
  vector<int> in_deg(ns, 0);
  rep(i, n) sz[scc[i]]++;
  rep(i, n) chmin(Min[scc[i]], l[i]);
  rep(x, ns)
  {
    for (auto y : E[x])
      in_deg[y]++;
  }
  int ans = accumulate(all(l), 0);
  rep(i, ns)
  {
    if (in_deg[i] == 0 or sz[i] >= 2)
      ans += Min[i];
  }
  cout << setprecision(1) << double(ans) / 10.0 << "\n";
}

int main()
{
  solve();
}
0