公式
F - 友達の輪 / Circle of Friends 解説
by
F - 友達の輪 / Circle of Friends 解説
by
physics0523
この問題には、 DSU(Union-Find) をほぼそのまま活用できます。
- 種類 \(1\) : \(a,b\) が属する集合を併合する。
- 種類 \(2\) : \(x\) が属する集合のサイズを答える。
これらの操作そのものが DSU には実装されています。
実装例 (C++):
#include<bits/stdc++.h>
#include<atcoder/dsu>
using namespace std;
using namespace atcoder;
int main(){
int N,Q;
cin >> N >> Q;
dsu d(N+1);
while(Q--){
int typ;
cin >> typ;
if(typ==1){
int a,b;
cin >> a >> b;
d.merge(a,b);
}
else{
int x;
cin >> x;
cout << d.size(x) << "\n";
}
}
return 0;
}
投稿日時:
最終更新:
