#include #include using namespace atcoder; using namespace std; using ll=long long; using ld=double; ld pie=3.14159265359; ll mod=998244353; long long inf=100000000000000001; struct unionfind { vectorpar,siz; unionfind(ll n): par(n,-1),siz(n,1){} ll root(ll x){ if (par[x]==-1) { return x; }else{ return par[x]=root(par[x]); } } bool issame(ll x,ll y){ return root(x)==root(y); } bool unite(ll x,ll y){ x=root(x); y=root(y); if (x==y) { return false; } if (siz[x]> n >> m; vectoru(m),v(m); ll ans=0; unionfind uf(n); vectormemo(n,0); for (ll i = 0; i < m; i++) { cin >> u[i] >> v[i]; u[i]--,v[i]--; } reverse(u.begin(),u.end()); reverse(v.begin(),v.end()); for (ll i = 0; i < m; i++) { ll x=max(memo[uf.root(u[i])],memo[uf.root(v[i])]); memo[uf.root(u[i])]=0; memo[uf.root(v[i])]=0; uf.unite(u[i],v[i]); memo[uf.root(u[i])]=x+1; } for (ll i = 0; i < memo.size(); i++) { ans+=memo[i]; } cout << ans << endl; }