結果

問題 No.1390 Get together
ユーザー ShibuyapShibuyap
提出日時 2021-02-19 17:42:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 142 ms / 2,000 ms
コード長 1,614 bytes
コンパイル時間 2,116 ms
コンパイル使用メモリ 202,720 KB
実行使用メモリ 14,380 KB
最終ジャッジ日時 2023-10-14 12:19:50
合計ジャッジ時間 7,804 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
10,424 KB
testcase_01 AC 5 ms
10,324 KB
testcase_02 AC 4 ms
10,484 KB
testcase_03 AC 7 ms
10,404 KB
testcase_04 AC 6 ms
10,416 KB
testcase_05 AC 6 ms
10,504 KB
testcase_06 AC 6 ms
10,400 KB
testcase_07 AC 6 ms
10,428 KB
testcase_08 AC 6 ms
10,400 KB
testcase_09 AC 8 ms
10,620 KB
testcase_10 AC 4 ms
10,528 KB
testcase_11 AC 4 ms
10,356 KB
testcase_12 AC 4 ms
10,368 KB
testcase_13 AC 5 ms
10,368 KB
testcase_14 AC 4 ms
10,360 KB
testcase_15 AC 4 ms
10,356 KB
testcase_16 AC 108 ms
11,124 KB
testcase_17 AC 130 ms
14,304 KB
testcase_18 AC 117 ms
11,124 KB
testcase_19 AC 134 ms
14,280 KB
testcase_20 AC 142 ms
14,300 KB
testcase_21 AC 136 ms
14,340 KB
testcase_22 AC 122 ms
12,892 KB
testcase_23 AC 133 ms
13,068 KB
testcase_24 AC 128 ms
13,052 KB
testcase_25 AC 134 ms
14,284 KB
testcase_26 AC 134 ms
14,336 KB
testcase_27 AC 132 ms
14,380 KB
testcase_28 AC 134 ms
14,328 KB
testcase_29 AC 137 ms
14,356 KB
testcase_30 AC 138 ms
14,332 KB
testcase_31 AC 135 ms
14,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}

const int MAX_N = 200100;
int par[MAX_N]; // 親
int rank_[MAX_N]; // 木の深さ
int cnt_[MAX_N]; // 属する頂点の個数(親のみ正しい)

// n要素で初期化
void UFinit(){
    for(int i=0;i<MAX_N;i++){
        par[i] = i;
        rank_[i] = 0;
        cnt_[i] = 1;
    }
}

// 木の根を求める
int find(int x){
    if(par[x] == x){
        return x;
    }else{
        return par[x] = find(par[x]);
    }
}

// xとyの属する集合を併合
void unite(int x, int y){
    x = find(x);
    y = find(y);
    if(x == y) return;

    if(rank_[x] < rank_[y]){
        par[x] = y;
        cnt_[y] += cnt_[x];
    }else{
        par[y] = x;
        cnt_[x] += cnt_[y];
        if(rank_[x] == rank_[y]) rank_[x]++;
    }
}

// xとyが同じ集合に属するか否か
bool same(int x, int y){
    return find(x) == find(y);
}


vector<int> v[MAX_N];

int main() {
    UFinit();
    ll n, m; cin >> n >> m;
    rep(i,n){
        int b, c;
        cin >> b >> c;
        v[c].push_back(b);
    }

    int ans = 0;

    rep(i,MAX_N){
        if(v[i].size() >= 2){
            rep(j,v[i].size()-1){
                if(same(v[i][j], v[i][j+1]) == 0){
                    ans++;
                    unite(v[i][j], v[i][j+1]);
                }
            }
        }
    }

    cout << ans << endl;
    return 0;
}
 
 
0