結果

問題 No.1390 Get together
ユーザー shiomusubi496shiomusubi496
提出日時 2021-02-12 21:29:53
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 738 bytes
コンパイル時間 2,065 ms
コンパイル使用メモリ 170,476 KB
実行使用メモリ 13,776 KB
最終ジャッジ日時 2023-09-27 02:55:44
合計ジャッジ時間 6,315 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 4 ms
4,384 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 4 ms
4,384 KB
testcase_06 AC 4 ms
4,384 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 4 ms
4,384 KB
testcase_09 AC 5 ms
4,384 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 111 ms
10,896 KB
testcase_17 AC 155 ms
13,440 KB
testcase_18 AC 122 ms
10,772 KB
testcase_19 AC 161 ms
13,776 KB
testcase_20 AC 163 ms
13,568 KB
testcase_21 AC 166 ms
13,448 KB
testcase_22 AC 141 ms
12,432 KB
testcase_23 AC 147 ms
12,596 KB
testcase_24 AC 146 ms
12,428 KB
testcase_25 AC 164 ms
13,460 KB
testcase_26 AC 165 ms
13,472 KB
testcase_27 AC 163 ms
13,504 KB
testcase_28 AC 163 ms
13,588 KB
testcase_29 AC 162 ms
13,500 KB
testcase_30 AC 161 ms
13,520 KB
testcase_31 AC 162 ms
13,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define int long long
using namespace std;
struct UnionFind{
    vector<int>par;
    UnionFind(int n):par(n,-1){}
    int find(int x){return par[x]<0?x:par[x]=find(par[x]);}
    void merge(int x,int y){
        x=find(x);y=find(y);
        if(x==y)return;
        if(par[x]>par[y])swap(x,y);
        par[x]+=par[y];par[y]=x;
    }
    int size(int x){return max(-par[x],1ll);}
};
signed main(){
    int N,M;cin>>N>>M;
    vector<vector<int>>V(N);
    for(int i=0;i<N;i++){
        int b,c;cin>>b>>c;
        V[--c].push_back(--b);
    }
    UnionFind UF(M);
    for(int i=0;i<N;i++){
        for(int j:V[i])UF.merge(j,V[i][0]);
    }
    int ans=0;
    for(int i=0;i<M;i++)ans+=UF.size(i)-1;
    cout<<ans<<endl;
}
0