unionFind u; int d[3d5]; { int @n,@m; int z=0; u.walloc(m+1,1); rep(n){ int @b,@c; if(d[c]==0){ d[c]=b; }else{ z+=u(d[c],b); } } wt(z); }