結果

問題 No.19 ステージの選択
ユーザー drymousedrymouse
提出日時 2024-02-17 16:23:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,399 bytes
コンパイル時間 949 ms
コンパイル使用メモリ 77,296 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-02-17 16:23:51
合計ジャッジ時間 2,179 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

int main(void) {
    int N;
    cin >> N;
    int L[N], S[N];
    for (int i = 0; i < N; i++) {
        int level, stage;
        cin >> level >> stage;
        L[i] = level << 1;
        S[i] = stage - 1;
    }
    vector<int> children[N];
    int tansaku[N] = {0};
    for (int i = 0; i < N; i++) {
        children[S[i]].push_back(i);
    }
    int graph_id = 1;
    int sum = 0;
    for (int i = 0; i < N; i++) {
        if (tansaku[i] == 0) {
            int takinobori[N] = {0};
            vector<int> passed;
            takinobori[i] = 1;
            passed.push_back(i);
            int cur = i, next, kon;
            for (;1;) {
                next = S[cur];
                if (next == cur) {
                    kon = cur;
                    break;
                } else if (takinobori[next] == 1) {
                    int min = L[next];
                    int minver = next;
                    int found = 0;
                    int lsize = passed.size();
                    for (int j = 0; j < lsize; j++) {
                        if (found) {
                            if (L[passed[j]] < min) {
                                minver = passed[j];
                                min = L[minver];
                            }
                        }else if (passed[j] == next) {
                            found = 1;
                        }
                    }
                    kon = minver;
                    break;
                } else {
                    takinobori[next] = 1;
                    passed.push_back(next);
                    cur = next;
                }
            }
            vector<int> que = {kon};
            tansaku[kon] = graph_id;
            sum += L[kon];
            for (;que.size();) {
                int cur = que[0];
                que.erase(que.begin());
                int csize = children[cur].size();
                for (int j = 0; j < csize; j++) {
                    int child = children[cur][j];
                    if (tansaku[child] == 0) {
                        tansaku[child] = graph_id;
                        que.push_back(child);
                        sum += L[child] / 2;
                    }
                }
            }
            graph_id++;
        }
    }

    cout << sum / 2 << "." << (sum % 2)*5 << endl;
    return 0;
}
0