This commit is contained in:
Zengtudor 2024-11-23 23:07:43 +08:00
parent a001973d25
commit 7dde412ebf

View File

@ -52,7 +52,24 @@ namespace s1{
namespace s2{
char c[maxn];
}
namespace s3{
char c[maxn];
struct USF{
ll f[maxn];
USF(){
for(ll i{1};i<=n;i++){
f[i]=i;
}
}
ll find(ll const&u){
if(f[u]==u)return u;
return f[u]=find(f[u]);
}
void merge(ll const &u,ll const &v){
f[find(u)]=find(v);
}
}usf;
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
@ -89,6 +106,38 @@ int main(){
cout<<ans<<'\n';
}
}else if(c==5||c==6){
while(t--){
cin>>n>>m;
memset(s3::c, 'T', sizeof(s3::c));
ll ans{};
for(ll i{1};i<=m;i++){
cin>>v[i];
if(v[i]=='U'){
cin>>tmp;
s3::c[tmp]='U';
}else{
cin>>vi[i]>>vj[i];
s3::usf.merge(vi[i],vj[i]);
s3::c[vi[i]]=s3::c[vj[i]];
}
}
unordered_map<ll,pair<ll,bool>> mm;
for(ll i{1};i<=n;i++){
mm[s3::usf.find(i)].first++;
if(s3::c[i]=='U'){
mm[s3::usf.find(i)].second=true;
}
}
for(auto [k,v]:mm){
if(v.second){
ans+=v.first;
}
}
cout<<ans<<'\n';
}
}
// else if(c==7||c==8){
// }
}