結果

問題 No.19 ステージの選択
ユーザー renjyaku_intrenjyaku_int
提出日時 2020-10-31 20:58:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,915 bytes
コンパイル時間 2,427 ms
コンパイル使用メモリ 212,696 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-29 10:40:16
合計ジャッジ時間 3,829 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//#include <tourist>
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> p;
const int INF = 1e9;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)          \
    cout << #v << ":";    \
    for (auto x : v)      \
    {                     \
        cout << x << ' '; \
    }                     \
    cout << endl;
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 (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//g++ yarudake.cpp -std=c++17 -I .
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
struct StronglyConnectedComponents
{
    vector<vector<int>> g;
    vector<vector<int>> to, from;
    vector<int> comp, order, used;
    StronglyConnectedComponents(vector<vector<int>> &g) : g(g), to(g.size()), from(g.size()), comp(g.size(), -1), used(g.size())
    {
        for (int i = 0; i < g.size(); i++)
        {
            for (auto x : g[i])
            {
                to[i].push_back(x);
                from[x].push_back(i);
            }
        }
    }
    int operator[](int k)
    {
        return comp[k];
    }
    void dfs(int now)
    {
        if (used[now])
            return;
        used[now] = true;
        for (int x : to[now])
            dfs(x);
        order.push_back(now);
    }
    void rdfs(int now, int cnt)
    {
        if (comp[now] != -1)
            return;
        comp[now] = cnt;
        for (int x : from[now])
            rdfs(x, cnt);
    }
    vector<vector<int>> build()
    {
        vector<vector<int>> t;
        for (int i = 0; i < to.size(); i++)
            dfs(i);
        reverse(order.begin(), order.end());
        int ptr = 0;
        for (int i : order)
            if (comp[i] == -1)
                rdfs(i, ptr), ptr++;

        t.resize(ptr);
        for (int i = 0; i < g.size(); i++)
        {
            for (auto v : to[i])
            {
                int x = comp[i], y = comp[v];
                if (x == y)
                    continue;
                t[x].push_back(y);
            }
        }
        return t;
    }
};
vector<double> l;
void dfs(int k, vector<vector<int>> &t, vector<vector<int>> &u, vector<bool> &used, double &ans)
{
    used[k] = true;
    rep(i,u[k].size()){
        ans += l[u[k][i]]/2;
    }
    rep(i,t[k].size()){
        dfs(t[k][i],t,u,used,ans);
    }
    return;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    vector<int> s(n);
    l.resize(n);
    rep(i, n)
    {
        cin >> l[i] >> s[i];
        s[i]--;
        g[s[i]].push_back(i);
    }

    StronglyConnectedComponents scc(g);
    vector<vector<int>> t = scc.build();
    //debug(scc.comp);

    vector<vector<int>> u(t.size()); //u[i]:iに含まれる成分
    rep(i, n)
    {
        u[scc[i]].push_back(i);
    }
    vector<bool> used(t.size(), false);
    double ans = 0;
    rep(i, t.size())
    {
        if (!used[i])
        {
            double temp = 100000000;
            rep(j, u[i].size())
            {
                chmin(temp, l[u[i][j]]);
            }
            ans += temp / 2;
            dfs(i, t, u, used, ans);
        }
    }
    cout<<ans<<"\n";
}
0