結果

問題 No.860 買い物
ユーザー mamekinmamekin
提出日時 2019-08-10 12:39:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 116 ms / 1,000 ms
コード長 2,487 bytes
コンパイル時間 2,125 ms
コンパイル使用メモリ 131,576 KB
実行使用メモリ 8,128 KB
最終ジャッジ日時 2023-09-26 23:20:59
合計ジャッジ時間 4,350 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 5 ms
4,384 KB
testcase_07 AC 116 ms
7,576 KB
testcase_08 AC 102 ms
7,760 KB
testcase_09 AC 103 ms
7,432 KB
testcase_10 AC 103 ms
7,384 KB
testcase_11 AC 70 ms
7,500 KB
testcase_12 AC 67 ms
7,508 KB
testcase_13 AC 100 ms
7,564 KB
testcase_14 AC 102 ms
8,128 KB
testcase_15 AC 51 ms
7,772 KB
testcase_16 AC 83 ms
7,916 KB
testcase_17 AC 114 ms
7,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;

class UnionFindTree
{
private:
    int n;
    int groupNum;       // グループの数
    vector<int> parent; // 親ノード
    vector<int> rank;   // 木の高さの上限
    vector<int> num;    // グループの要素数
    int find(int i){
        if(parent[i] == i)
            return i;
        else
            return parent[i] = find(parent[i]);
    }
public:
    UnionFindTree(int n){ // コンストラクタ
        this->n = n;
        groupNum = n;
        parent.resize(n);
        for(int i=0; i<n; ++i)
            parent[i] = i;
        rank.assign(n, 0);
        num.assign(n, 1);
    }
    void unite(int a, int b){ // aとbのグループを併合
        if((a = find(a)) != (b = find(b))){
            if(rank[a] < rank[b]){
                parent[a] = b;
                num[b] += num[a];
            }
            else{
                parent[b] = a;
                if(rank[a] == rank[b])
                    ++ rank[a];
                num[a] += num[b];
            }
            -- groupNum;
        }
    }
    bool same(int a, int b){ // aとbのグループが同じかを調べる
        return find(a) == find(b);
    }
    int getNum(){ // グループの数を返す
        return groupNum;
    }
    int getNum(int a){ // aのグループの要素数を返す
        return num[find(a)];
    }
};

int main()
{
    int n;
    cin >> n;
    vector<int> c(n), d(n);
    for(int i=0; i<n; ++i)
        cin >> c[i] >> d[i];

    vector<tuple<int, int, int> > v;
    for(int i=0; i<n; ++i)
        v.push_back(make_tuple(c[i], i, n));
    for(int i=1; i<n; ++i)
        v.push_back(make_tuple(d[i], i-1, i));
    sort(v.begin(), v.end());

    UnionFindTree uft(n+1);
    long long ans = accumulate(c.begin(), c.end(), 0LL);
    for(const auto& t: v){
        int cost, i, j;
        tie(cost, i, j) = t;
        if(!uft.same(i, j)){
            ans += cost;
            uft.unite(i, j);
        }
    }
    cout << ans << endl;

    return 0;
}
0