結果

問題 No.1390 Get together
ユーザー seiyu_kyoproseiyu_kyopro
提出日時 2021-02-12 22:25:39
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 173 ms / 2,000 ms
コード長 3,129 bytes
コンパイル時間 1,823 ms
コンパイル使用メモリ 177,548 KB
実行使用メモリ 20,360 KB
最終ジャッジ日時 2023-09-27 04:59:30
合計ジャッジ時間 5,885 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 124 ms
16,172 KB
testcase_17 AC 95 ms
12,480 KB
testcase_18 AC 173 ms
20,360 KB
testcase_19 AC 108 ms
12,428 KB
testcase_20 AC 106 ms
12,348 KB
testcase_21 AC 106 ms
12,580 KB
testcase_22 AC 120 ms
16,856 KB
testcase_23 AC 121 ms
16,796 KB
testcase_24 AC 119 ms
16,600 KB
testcase_25 AC 110 ms
12,428 KB
testcase_26 AC 108 ms
12,428 KB
testcase_27 AC 107 ms
12,428 KB
testcase_28 AC 108 ms
12,484 KB
testcase_29 AC 109 ms
12,520 KB
testcase_30 AC 108 ms
12,488 KB
testcase_31 AC 105 ms
12,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define rrep(i, n) for (int i = (int)n-1; i >= 0; --i)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9+7;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

__attribute__ ((constructor))
void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
}

struct dsu {
    public:
        dsu() : _n(0) {}
        dsu(int n) : _n(n), parent_or_size(n, -1) {}
        int merge(int a, int b) {
            assert(0 <= a && a < _n);
            assert(0 <= b && b < _n);
            int x = leader(a), y = leader(b);
            if (x == y) return x;
            if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
            parent_or_size[x] += parent_or_size[y];
            parent_or_size[y] = x;
            return x;
        }
        bool same(int a, int b) {
            assert(0 <= a && a < _n);
            assert(0 <= b && b < _n);
            return leader(a) == leader(b);
        }
        int leader(int a) {
            assert(0 <= a && a < _n);
            if (parent_or_size[a] < 0) return a;
            return parent_or_size[a] = leader(parent_or_size[a]);
        }
        int size(int a) {
            assert(0 <= a && a < _n);
            return -parent_or_size[leader(a)];
        }
        vector<vector<int>> groups() {
            vector<int> leader_buf(_n), group_size(_n);
            for (int i = 0; i < _n; i++) {
                leader_buf[i] = leader(i);
                group_size[leader_buf[i]]++;
            }
            vector<std::vector<int>> result(_n);
            for (int i = 0; i < _n; i++) {
                result[i].reserve(group_size[i]);
            }
            for (int i = 0; i < _n; i++) {
                result[leader_buf[i]].push_back(i);
            }
            result.erase(
                remove_if(result.begin(), result.end(),
                    [&](const vector<int>& v) { return v.empty(); }),
                result.end());
            return result;
        }
    private:
        int _n;
        // root node: -1 * component size
        // otherwise: parent
        vector<int> parent_or_size;
};

int main(){
    int n, m;

    cin >> n >> m;

    vector<int> slim[n];

    dsu d(m);

    rep(i, n){
        int b,c;
        cin >> b >> c;
        b--;
        c--;
        slim[c].push_back(b);
    }

    ll ans = 0;

    rep(i,n){
        set<int> si;
        for(auto u : slim[i]){
            si.insert(d.leader(u));
        }
        if(int(si.size()) == 0)
            continue;
        ans += int(si.size()) - 1;
        vector<int> lea;
        for(auto u : si)
            lea.push_back(u);

        rep(i, int(lea.size())) { d.merge(lea[0], lea[i]); }
    }

    cout << ans << endl;

    return 0;
}
0