CSES - Shared codeLink to this code: https://cses.fi/paste/d177f38d5d625174681c04/
#include<iostream>
#include<array>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
class DSU{
int largestComponentSize,compSize;
vector<int>parent,weight;
int find(int city){
if(parent[city]==city)return city;
return parent[city]=find(parent[city]);
}
public:
DSU(int n){
largestComponentSize=0;
parent.resize(n);
compSize=n-1;
weight.resize(n,1);
for(int i=1;i<n;i++){
parent[i]=i;
}
}
bool Union(int a,int b){
a=find(a);
b=find(b);
if(a==b)return false;
if(weight[a]>weight[b])swap(a,b);
weight[b]+=weight[a];
parent[a]=b;
compSize--;
largestComponentSize=max(largestComponentSize,weight[b]);
return true;
}
int getSize(){
return compSize;
}
int getLargest(){
return largestComponentSize;
}
};
int main(){
int n,m,a,b;
cin>>n>>m;
DSU dsu(n+1);
while(m--){
cin>>a>>b;
dsu.Union(a,b);
cout<<dsu.getSize()<<" "<<dsu.getLargest()<<"\n";
}
return 0;
}