結果

問題 No.1390 Get together
ユーザー 👑 deuteridayodeuteridayo
提出日時 2021-02-12 21:45:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 157 ms / 2,000 ms
コード長 2,909 bytes
コンパイル時間 1,875 ms
コンパイル使用メモリ 176,204 KB
実行使用メモリ 17,552 KB
最終ジャッジ日時 2023-09-27 03:30:23
合計ジャッジ時間 5,824 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 114 ms
14,876 KB
testcase_17 AC 147 ms
17,432 KB
testcase_18 AC 124 ms
14,724 KB
testcase_19 AC 150 ms
17,440 KB
testcase_20 AC 154 ms
17,552 KB
testcase_21 AC 155 ms
17,436 KB
testcase_22 AC 130 ms
16,540 KB
testcase_23 AC 137 ms
16,532 KB
testcase_24 AC 139 ms
16,336 KB
testcase_25 AC 155 ms
17,404 KB
testcase_26 AC 155 ms
17,408 KB
testcase_27 AC 157 ms
17,492 KB
testcase_28 AC 156 ms
17,480 KB
testcase_29 AC 154 ms
17,496 KB
testcase_30 AC 152 ms
17,480 KB
testcase_31 AC 151 ms
17,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
// #include<atcoder/all>
using namespace std;
using lint = long long;
template<class T>using graph = vector<vector<T>>;
#define endl '\n'
int const INF = 1<<30;
lint const INF64 = 1LL<<62;
lint const mod = 1e9+7;
//long const mod = 998244353;
#ifndef ATCODER_DSU_HPP
#define ATCODER_DSU_HPP 1

#include <algorithm>
#include <cassert>
#include <vector>

namespace atcoder {

// Implement (union by size) + (path compression)
// Reference:
// Zvi Galil and Giuseppe F. Italiano,
// Data structures and algorithms for disjoint set union problems
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)];
    }

    std::vector<std::vector<int>> groups() {
        std::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]]++;
        }
        std::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(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

}  // namespace atcoder

#endif  // ATCODER_DSU_HPP


int main(){

using namespace atcoder;
    lint n,m;
    cin >> n >> m;
    lint b[n],c[n];
    for(int i=0;i<n;i++){
        cin >> b[i]  >> c[i];
        b[i]--;
        c[i]--;
    }
    vector<vector<lint>>colors(n);
    vector<lint>cnt(n);
    for(int i=0;i<n;i++){
        colors[c[i]].push_back(b[i]);
    }
    lint ans = 0;
    dsu d(m);
    for(int i=0;i<n;i++){
        for(int j=0;j+1<colors[i].size();j++){
            if(!d.same(colors[i][j], colors[i][j+1])){
                d.merge(colors[i][j], colors[i][j+1]);
                ans++;
            }
        }
    }
    cout << ans << endl;
}

0