From 7dde412ebf7cb966fe1b6ab6a78b9c678e46fa8b Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Sat, 23 Nov 2024 23:07:43 +0800 Subject: [PATCH] update --- src/P9869/P9869.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/src/P9869/P9869.cpp b/src/P9869/P9869.cpp index 5319f3f..4bee054 100644 --- a/src/P9869/P9869.cpp +++ b/src/P9869/P9869.cpp @@ -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<>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> 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<