Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- using namespace std;
- class Modunionfind{
- int n;
- vector<int> roots;
- vector<int> sums;
- vector<set<int>> elements;
- public:
- Modunionfind(int N):n(N){
- roots.resize(n);
- sums.resize(n);
- elements.resize(n);
- for (int i=0;i!=n;++i ){roots[i]=i;elements[i].insert(i);sums[i]=i+1;}
- };
- int get_size(int x){return elements[roots[x]].size();}
- int get_sum(int x){return sums[roots[x]];}
- void unite(int from, int to){
- if (roots[from]==roots[to]) return;
- if (get_size(from)>get_size(to)) swap(from,to);
- sums[roots[to]]+=sums[roots[from]];
- int rf=roots[from];
- for (int x:elements[rf]){
- elements[roots[to]].insert(x);
- roots[x]=roots[to];
- }
- //elements[rf].clear();
- }
- void move(int from, int to){
- if (roots[from]==roots[to]) return;
- elements[roots[from]].erase(from);
- sums[roots[from]]-=from+1;
- roots[from]=roots[to];
- sums[roots[to]]+=from+1;
- elements[roots[to]].insert(from);
- }
- };
- void solve(int n,int m){
- Modunionfind mf(n);
- for (int i=0; i!=m;++i){
- int a;
- cin>>a;
- if (a==1){
- int b,c;
- cin>>b>>c;
- --b;--c;
- mf.unite(b,c);
- }
- else if (a==2){
- int b,c;
- cin>>b>>c;
- --b;--c;
- mf.move(b,c);
- }else{
- int b;
- cin>>b;
- --b;
- cout<<mf.get_size(b)<<" "<<mf.get_sum(b)<<"\n";
- }
- }
- }
- int main(){
- ios::sync_with_stdio(0);
- int n,m;
- while (cin>>n>>m)solve(n,m);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement