結果

問題 No.19 ステージの選択
ユーザー maine_honzukimaine_honzuki
提出日時 2020-05-05 13:09:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,146 bytes
コンパイル時間 2,044 ms
コンパイル使用メモリ 181,700 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 14:29:53
合計ジャッジ時間 3,629 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

struct StronglyConnectedComponent {
public:
    vector<vector<int>> G, R, T;
    //
private:
    vector<int> cmp, ord;
    vector<bool> used;
    //
public:
    StronglyConnectedComponent(int n) : G(n), R(n), cmp(n, -1), used(n, false) {}
    void add_edge(int u, int v) {
        G[u].emplace_back(v);
        R[v].emplace_back(u);
    }
    //
private:
    void dfs(int now) {
        if (used[now])
            return;
        used[now] = true;
        for (auto nxt : G[now])
            dfs(nxt);
        ord.emplace_back(now);
    }
    void rdfs(int now, int count) {
        if (cmp[now] != -1)
            return;
        cmp[now] = count;
        for (auto to : R[now])
            rdfs(to, count);
    }
    //
public:
    int build() {
        int n = (int)G.size();
        for (int i = 0; i < n; i++)
            dfs(i);
        reverse(ord.begin(), ord.end());
        int group = 0;
        for (auto& i : ord) {
            if (cmp[i] == -1) {
                rdfs(i, group);
                group++;
            }
        }
        T.resize(group);
        for (int i = 0; i < n; i++) {
            for (auto& to : G[i]) {
                int s = cmp[i], t = cmp[to];
                if (s != t)
                    T[s].emplace_back(t);
            }
        }
        return group;
    }
    int operator[](int k) { return cmp[k]; }
};
using SCC = StronglyConnectedComponent;

//
int main() {
    int N, L[101], S[101];
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> L[i] >> S[i];
        S[i]--;
    }

    SCC scc(N);
    for (int i = 0; i < N; i++) {
        scc.add_edge(S[i], i);
    }
    scc.build();

    map<int, int> M;
    for (int i = 0; i < N; i++) {
        if (scc[i] == scc[S[i]]) {
            if (M.count(scc[i]))
                M[scc[i]] = min(M[scc[i]], L[i]);
            else
                M[scc[i]] = L[i];
        }
    }

    double ans = 0;
    for (int i = 0; i < N; i++) {
        ans += L[i] / 2.0;
    }
    for (auto& p : M) {
        ans += p.second / 2.0;
    }
    cout << fixed << setprecision(1);
    cout << ans << endl;
}
0