あってる、もっというと以下でいい
auto g = uf.groups();
int ans = m - (n - g.size());