結果

問題 No.1390 Get together
ユーザー bonKotsubonKotsu
提出日時 2021-05-26 00:13:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 173 ms / 2,000 ms
コード長 2,341 bytes
コンパイル時間 1,972 ms
コンパイル使用メモリ 175,420 KB
実行使用メモリ 15,076 KB
最終ジャッジ日時 2024-04-22 15:56:54
合計ジャッジ時間 7,233 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 127 ms
11,004 KB
testcase_17 AC 152 ms
14,976 KB
testcase_18 AC 136 ms
11,024 KB
testcase_19 AC 164 ms
14,976 KB
testcase_20 AC 163 ms
14,924 KB
testcase_21 AC 158 ms
14,844 KB
testcase_22 AC 147 ms
13,568 KB
testcase_23 AC 161 ms
13,500 KB
testcase_24 AC 158 ms
13,300 KB
testcase_25 AC 173 ms
14,972 KB
testcase_26 AC 167 ms
14,976 KB
testcase_27 AC 166 ms
15,000 KB
testcase_28 AC 170 ms
15,044 KB
testcase_29 AC 167 ms
14,832 KB
testcase_30 AC 171 ms
15,040 KB
testcase_31 AC 166 ms
15,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define P pair<int, int>


//以下、素集合と木は同じものを表す
class UnionFind{
public:
    vector<ll> parent; //parent[i]はiの親
    vector<ll> siz; //素集合のサイズを表す配列(1で初期化)
    map<ll,vector<ll>> group; //集合ごとに管理する(key:集合の代表元、value:集合の要素の配列)
    ll n; //要素数

    //コンストラクタ
    UnionFind(ll n_):n(n_),parent(n_),siz(n_,1){ 
        //全ての要素の根が自身であるとして初期化
        for(ll i=0;i<n;i++){parent[i]=i;}
    }

    //データxの属する木の根を取得(経路圧縮も行う)
    ll root(ll x){
        if(parent[x]==x) return x;
        return parent[x]=root(parent[x]);//代入式の値は代入した変数の値なので、経路圧縮できる
    }

    //xとyの木を併合
    void unite(ll x,ll y){
        ll rx=root(x);//xの根
        ll ry=root(y);//yの根
        if(rx==ry) return;//同じ木にある時
        //小さい集合を大きい集合へと併合(ry→rxへ併合)
        if(siz[rx]<siz[ry]) swap(rx,ry);
        siz[rx]+=siz[ry];
        parent[ry]=rx;//xとyが同じ木にない時はyの根ryをxの根rxにつける
    }

    //xとyが属する木が同じかを判定
    bool same(ll x,ll y){
        ll rx=root(x);
        ll ry=root(y);
        return rx==ry;
    }

    //xの素集合のサイズを取得
    ll size(ll x){
        return siz[root(x)];
    }

    //素集合をそれぞれグループ化
    void grouping(){
        //経路圧縮を先に行う
        rep(i,n)root(i);
        //mapで管理する(デフォルト構築を利用)
        rep(i,n)group[parent[i]].push_back(i);
    }

    //素集合系を削除して初期化
    void clear(){
        rep(i,n){parent[i]=i;}
        siz=vector<ll>(n,1);
        group.clear();
    }
};

int main() {
  int n, m;
  cin >> n >> m;

  vector<vector<int>> grp(n);
  UnionFind uf(m);
  rep(i,n) {
    int b, c;
    cin >> b >> c;
    b--, c--;
    if (grp[c].empty()) {
      grp[c].push_back(b);
    } else {
      uf.unite(grp[c].back(), b);
    }
  }

  int ans = 0;
  rep(i,m) {
    if (uf.root(i) == i) ans += uf.size(i)-1;
  }
  cout << ans << endl;


}
0