結果

問題 No.1390 Get together
ユーザー norikamenorikame
提出日時 2021-02-12 22:02:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 1,999 bytes
コンパイル時間 1,911 ms
コンパイル使用メモリ 203,640 KB
実行使用メモリ 12,712 KB
最終ジャッジ日時 2023-09-27 04:01:21
合計ジャッジ時間 5,886 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 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,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 106 ms
9,228 KB
testcase_17 AC 146 ms
12,508 KB
testcase_18 AC 115 ms
9,284 KB
testcase_19 AC 144 ms
12,504 KB
testcase_20 AC 149 ms
12,428 KB
testcase_21 AC 146 ms
12,712 KB
testcase_22 AC 130 ms
11,400 KB
testcase_23 AC 134 ms
11,392 KB
testcase_24 AC 137 ms
11,560 KB
testcase_25 AC 148 ms
12,440 KB
testcase_26 AC 153 ms
12,524 KB
testcase_27 AC 149 ms
12,372 KB
testcase_28 AC 153 ms
12,356 KB
testcase_29 AC 147 ms
12,352 KB
testcase_30 AC 148 ms
12,388 KB
testcase_31 AC 146 ms
12,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
#define v(t) vector<t>
#define p(t) pair<t, t>
#define p2(t, s) pair<t, s>
#define vp(t) v(p(t))

#define rep(i, n) for (int i=0,i##_len=((int)(n)); i<i##_len; ++i)
#define rep2(i, a, n) for (int i=((int)(a)),i##_len=((int)(n)); i<=i##_len; ++i)
#define repr(i, n) for (int i=((int)(n)-1); i>=0; --i)
#define rep2r(i, a, n) for (int i=((int)(n)),i##_len=((int)(a)); i>=i##_len; --i)

#define repi(itr, c) for (__typeof((c).begin()) itr=(c).begin(); itr!=(c).end(); ++itr)
#define repir(itr, c) for (__typeof((c).rbegin()) itr=(c).rbegin(); itr!=(c).rend(); ++itr)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define eb emplace_back

#define INF (1e9)
#define LINF (1e18)
#define PI (acos(-1))
#define EPS (1e-7)
#define DEPS (1e-10)

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

int main(){
    int n, m;
    cin >> n >> m;
    v(v(int)) col(n);
    rep(i, n) {
        int bi, ci;
        cin >> bi >> ci;
        --bi; --ci;
        col[ci].pb(bi);
    }
    UnionFind uf(m);
    rep(i, n) rep(j, sz(col[i])-1) uf.unite(col[i][j], col[i][j+1]);
    int cnt = 0;
    rep(i, m) if (uf.par[i] < 0) cnt += abs(uf.par[i]) - 1;
    cout << cnt << endl;
    return 0;
}
0