結果

問題 No.1390 Get together
ユーザー lorent_kyoprolorent_kyopro
提出日時 2021-02-12 22:26:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 221 ms / 2,000 ms
コード長 4,182 bytes
コンパイル時間 2,797 ms
コンパイル使用メモリ 210,976 KB
実行使用メモリ 19,244 KB
最終ジャッジ日時 2023-09-27 05:02:41
合計ジャッジ時間 7,239 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 41 ms
6,552 KB
testcase_17 AC 192 ms
19,216 KB
testcase_18 AC 42 ms
6,404 KB
testcase_19 AC 213 ms
19,244 KB
testcase_20 AC 204 ms
19,148 KB
testcase_21 AC 205 ms
19,172 KB
testcase_22 AC 118 ms
14,176 KB
testcase_23 AC 118 ms
14,348 KB
testcase_24 AC 122 ms
14,252 KB
testcase_25 AC 212 ms
19,144 KB
testcase_26 AC 215 ms
19,160 KB
testcase_27 AC 217 ms
19,172 KB
testcase_28 AC 218 ms
19,212 KB
testcase_29 AC 219 ms
19,196 KB
testcase_30 AC 221 ms
19,240 KB
testcase_31 AC 220 ms
19,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define ALL(v) v.begin(), v.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
const array<int, 8> di = {0, 1, 0, -1, 1, 1, -1, -1}, dj = {1, 0, -1, 0, 1, -1, -1, 1};
const array<string, 2> YESNO = {"NO", "YES"};
const array<string, 2> YesNo = {"No", "Yes"};
const array<string, 2> yesno = {"no", "yes"};
void YES(bool b = true) { cout << YESNO[b] << '\n'; }
void Yes(bool b = true) { cout << YesNo[b] << '\n'; }
void yes(bool b = true) { cout << yesno[b] << '\n'; }
template<typename T, typename U>
inline bool chmax(T& a, const U& b) {
    if (a < b){
        a = b;
        return true;
    }
    return false;
}
template<typename T, typename U>
inline bool chmin(T& a, const U& b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<typename T>
T MAX(const vector<T>& v) { return *max_element(v.begin(), v.end()); }
template<typename T>
T MIN(const vector<T>& v) { return *min_element(v.begin(), v.end()); }
template<typename T>
T SUM(const vector<T>& v) { return accumulate(v.begin(), v.end(), T()); }
template<typename T>
void UNIQUE(vector<T>& v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
}
template<typename T>
T POW(T a, int n) {
    T res = 1;
    for (; n > 0; n >>= 1, a *= a) if (n & 1) res *= a;
    return res;
}
template<typename T>
int lb(const vector<T>& v, T x) {
    return distance(v.begin(), lower_bound(v.begin(), v.end(), x));
}
template<typename T>
int ub(const vector<T>& v, T x) {
    return distance(v.begin(), upper_bound(v.begin(), v.end(), x));
}
/**
 * @brief 多次元 vector の作成
 * @author えびちゃん
 */
namespace detail {
    template<typename T, int N>
    auto make_vec(vector<int>& sizes, T const& x) {
        if constexpr (N == 1) {
            return vector(sizes[0], x);
        } else {
            int size = sizes[N-1];
            sizes.pop_back();
            return vector(size, make_vec<T, N-1>(sizes, x));
        }
    }
}
template<typename T, int N>
auto make_vec(int const(&sizes)[N], T const& x = T()) {
    vector<int> s(N);
    for (int i = 0; i < N; ++i) s[i] = sizes[N-i-1];
    return detail::make_vec<T, N>(s, x);
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        if (it == v.begin()) os << *it;
        else os << ' ' << *it;
    }
    return os;
}
template<typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << p.first << ' ' << p.second;
    return os;
}
__attribute__((constructor))
void setup() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
}
// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353;

struct unionfind {
    vector<int> d;
    unionfind(int n) : d(n, -1) {}
    int find(int x) {
        if (d[x] < 0) return x;
        return d[x] = find(d[x]);
    }
    bool unite(int x, int y) {
        x = find(x); y = find(y);
        if (x == y) return false;
        if (d[x] > d[y]) swap(x, y);
        d[x] += d[y];
        d[y] = x;
        return true;
    }
    bool same(int x, int y) { return find(x) == find(y); }
    int size(int x) { return -d[find(x)]; }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> box(n), color(n);
    rep(i, n) cin >> box[i] >> color[i], box[i]--, color[i]--;
    unionfind uf(m);
    map<int, vector<int>> mp;
    rep(i, n) mp[color[i]].pb(box[i]);
    for (auto& [c, v] : mp) {
        rep(i, v.size() - 1) uf.unite(v[i], v[i+1]);
    }

    ll ans = 0;
    rep(i, m) {
        if (uf.find(i) == i) {
            ans += uf.size(i) - 1;
        }
    }
    cout << ans << '\n';

    // for (auto& [b, v] : mp) {
    //     sort(ALL(v));
    //     cout << b << " : " << v << '\n';
    // }
}
0