結果

問題 No.19 ステージの選択
ユーザー fumiphysfumiphys
提出日時 2019-04-04 22:35:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,454 bytes
コンパイル時間 1,294 ms
コンパイル使用メモリ 120,184 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-27 06:07:21
合計ジャッジ時間 2,446 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

// includes
#include <cstdio>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <utility>
#include <functional>
#include <cmath>
#include <climits>
#include <bitset>
#include <list>
#include <random>

// macros
#define ll long long int
#define pb emplace_back
#define mk make_pair
#define pq priority_queue
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())

using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;
 
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1e9 + 7;

// solve
template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}
template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}


int main(int argc, char const* argv[])
{
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> l(n), s(n);
  rep(i, n)cin >> l[i] >> s[i], s[i]--;
  vector<bool> used(n, false);
  vector<int> to(n, 1);
  vector<vector<int> > edge(n);
  stack<int> st;
  ll res = 0;
  int cnt = 0;
  rep(i, n){
    if(i != s[i]){
      edge[s[i]].pb(i);
    }
  }
  rep(i, n){
    if(i == s[i])st.push(i);
  }
  while(cnt < n){
    if(st.size()){
      int x = st.top(); st.pop();
      if(used[s[x]])res += l[x];
      else res += l[x] * 2;
      used[x] = true;
      for(auto e: edge[x]){
        to[e]--;
        if(to[e] == 0 && !used[e])st.push(e);
      }
      cnt++;
    }else{
      rep(i, n){
        if(used[i])continue;
        vector<bool> vis(n, false);
        int curr = i;
        while(!vis[curr]){
          vis[curr] = true;
          curr = s[curr];
        }
        int tmp = curr;
        int at = curr;
        int mini = inf;
        do{
          if(mini > l[curr]){
            mini = l[curr];
            at = curr;
          }
          curr = s[curr];
        }while(tmp != curr);
        st.push(at);
        break;
      }
    }
  }
  cout << res / 2;
  if(res % 2 == 0)cout << ".0" << endl;
  else cout << ".5" << endl;
	return 0;
}
0