結果
問題 | No.1390 Get together |
ユーザー |
|
提出日時 | 2021-02-12 21:29:53 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 158 ms / 2,000 ms |
コード長 | 738 bytes |
コンパイル時間 | 1,639 ms |
コンパイル使用メモリ | 173,072 KB |
実行使用メモリ | 13,804 KB |
最終ジャッジ日時 | 2024-07-19 20:21:35 |
合計ジャッジ時間 | 5,543 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#include<bits/stdc++.h>#define int long longusing 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;}