結果

問題 No.19 ステージの選択
ユーザー Ymgch_KYmgch_K
提出日時 2017-06-22 10:05:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,396 bytes
コンパイル時間 1,789 ms
コンパイル使用メモリ 178,168 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-24 23:32:46
合計ジャッジ時間 2,799 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define OUT(x) cout << #x << " = " << x << endl
#define rep(i, n)             for (int (i) = 0; (i) < (int)(n); (i)++)
#define rer(i, l, r)          for (int (i) = (int)(l); (i) <= (int)(r); (i)++)
#define reu(i, l, r)          for (int (i) = (int)(l); (i) < (int)(r); (i)++)
#define all(x)                (x).begin(), (x).end()
#define rall(x)               (x).rbegin(), (x).rend()
template<typename T> void pv(T a, T b) { for (T i = a; i != b; i ++) cout << *i << " "; cout << endl; }
template<typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; }
int in() { int x; scanf("%d", &x); return x; }
long long lin() { long long x; scanf("%lld", &x); return x; }

int n;
vector<int> g[110];
vector<int> rg[110];
vector<int> order;
bool used[110];
int cmp[110];

void add_edge(int from, int to) {
        g[from].push_back(to);
        rg[to].push_back(from);
}
void dfs(int v) {
        used[v] = true;
        for (auto i : g[v]) if (!used[i]) dfs(i);
        order.push_back(v);
}
void rdfs(int v, int k) {
        used[v] = true;
        cmp[v] = k;
        for (auto i : rg[v]) if (!used[i]) rdfs(i, k);
}
int StronglyConnectedComponent() {
        memset(used, 0, sizeof(used));
        order.clear();
        for (int i = 0; i < n; i ++) if (!used[i]) dfs(i);
        memset(used, 0, sizeof(used));
        int k = 0;
        for (int i = order.size() - 1; i >= 0; i --) if (!used[order[i]]) rdfs(order[i], k ++);
        return k;
}

signed main() { 
        cin >> n;
        vector<int> L(n);
        rep(i, n) {
                cin >> L[i];
                int s;
                cin >> s;
                s --;
                if (s - i) add_edge(s, i);
        }
        int k = StronglyConnectedComponent();
        vector<pair<int, int>> cmmp(n);
        rep(i, n) {
                cmmp[i].first = cmp[i];
                cmmp[i].second = i;
        }
        sort(all(cmmp));
        double ans = 0.0;
        rep(i, n) {
                vector<int> can;
                can.push_back(cmmp[i].second);
                while (i < n - 1 && cmmp[i].first == cmmp[i + 1].first) {
                        can.push_back(cmmp[i + 1].second);
                        i ++;
                }
                if (can.size() == 1) {
                        bool f = false;
                        rep(j, rg[cmmp[i].second].size()) {
                                if (cmp[cmmp[i].second] > cmp[rg[cmmp[i].second][j]]) {
                                        f = true;
                                }
                        }
                        if (f) {
                                ans += (double)L[cmmp[i].second] / 2.0;
                        } else {
                                ans += (double)L[cmmp[i].second];
                        }
                } else {
                        vector<int> l;
                        rep(j, can.size()) {
                                int ll = L[can[j]];
                                l.push_back(ll);
                        }
                        sort(all(l));
                        ans += (double)l[0];
                        reu(j, 1, l.size()) ans += (double)l[j] / 2.0;
                }
        }
        printf("%.1f\n", ans);
        return 0;
}               
0