Submission #7125615


Source Code Expand

Copy
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
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; }
//---------------------------------------------------------------------------------------------------
struct UnionFind {
    vector<int> par; // uf(x,y)->y
    UnionFind() {}
    UnionFind(int NV) { init(NV); }
    void init(int NV) { par.clear(); rep(i, 0, NV) par.push_back(i); }
    void reset() { rep(i, 0, par.size()) par[i] = i; }
    int operator[](int x) { return par[x] == x ? x : par[x] = operator[](par[x]); }
    void operator()(int x, int y) { x = operator[](x); y = operator[](y); if (x != y) par[x] = y; }
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/














int N, H, W;
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> H >> W;
    vector<pair<int,pair<int,int>>> E;
    rep(i, 0, N) {
        int y, x, a; cin >> y >> x >> a;
        y--; x--;
        E.push_back({a, {x, y + W}});
    }

    UnionFind uf(H + W + 1);
    sort(all(E));
    reverse(all(E));

    ll ans = 0;
    fore(p, E) {
        int c = p.first;
        int a = p.second.first;
        int b = p.second.second;

        if(uf[a] == uf[b]) {
            if(uf[a] == uf[H + W]) continue;

            ans += c;
            uf(a, H + W);
        } else {
            ans += c;
            uf(a, b);
        }
    }
    cout << ans << endl;
}





Submission Info

Submission Time
Task E - Card Collector
User hamayanhamayan
Language C++14 (GCC 5.4.1)
Score 800
Code Size 2405 Byte
Status
Exec Time 37 ms
Memory 3572 KB

Test Cases

Set Name Score / Max Score Test Cases
All 800 / 800 sample_01, sample_02, sample_03, testcase_01, testcase_02, testcase_03, testcase_04, testcase_05, testcase_06, testcase_07, testcase_08, testcase_09, testcase_10, testcase_11, testcase_12, testcase_13, testcase_14, testcase_15, testcase_16, testcase_17, testcase_18, testcase_19, testcase_20, testcase_21, testcase_22, testcase_23, testcase_24, testcase_25, testcase_26, testcase_27, testcase_28, testcase_29, testcase_30, testcase_31, testcase_32, testcase_33, testcase_34, testcase_35, testcase_36, testcase_37
Sample 0 / 0 sample_01, sample_02, sample_03
Case Name Status Exec Time Memory
sample_01 1 ms 256 KB
sample_02 1 ms 256 KB
sample_03 2 ms 1400 KB
testcase_01 14 ms 1528 KB
testcase_02 22 ms 1912 KB
testcase_03 6 ms 1084 KB
testcase_04 36 ms 3188 KB
testcase_05 36 ms 3188 KB
testcase_06 16 ms 1272 KB
testcase_07 31 ms 3060 KB
testcase_08 23 ms 1784 KB
testcase_09 26 ms 1912 KB
testcase_10 34 ms 3188 KB
testcase_11 30 ms 1912 KB
testcase_12 37 ms 3188 KB
testcase_13 36 ms 3060 KB
testcase_14 36 ms 3060 KB
testcase_15 29 ms 2036 KB
testcase_16 19 ms 1656 KB
testcase_17 18 ms 1784 KB
testcase_18 34 ms 3188 KB
testcase_19 35 ms 3188 KB
testcase_20 35 ms 3188 KB
testcase_21 35 ms 3188 KB
testcase_22 36 ms 3188 KB
testcase_23 35 ms 3188 KB
testcase_24 2 ms 1400 KB
testcase_25 34 ms 2292 KB
testcase_26 32 ms 2292 KB
testcase_27 29 ms 1912 KB
testcase_28 36 ms 3572 KB
testcase_29 37 ms 3572 KB
testcase_30 36 ms 3188 KB
testcase_31 37 ms 3188 KB
testcase_32 1 ms 256 KB
testcase_33 36 ms 2548 KB
testcase_34 36 ms 3188 KB
testcase_35 35 ms 3188 KB
testcase_36 35 ms 3060 KB
testcase_37 35 ms 3060 KB