fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=1e5+5;
  4. int n,m;
  5. int p[N];
  6. int get(int u){
  7. return (u==p[u]?u:p[u]=get(p[u]));
  8. }
  9. int main()
  10. {
  11. ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  12. freopen("dsf.inp","r",stdin);
  13. freopen("dsf.out","w",stdout);
  14. cin>>n>>m;
  15. for(int i=1;i<=n;i++)p[i]=i;
  16. int scc=n;
  17. for(int i=1;i<=m;i++){
  18. int u,v;cin>>u>>v;
  19. u=get(u);v=get(v);
  20. if(u!=v){
  21. p[v]=u;
  22. scc--;
  23. }
  24. cout<<scc<<'\n';
  25. }
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0s 5324KB
stdin
4 4
1 2
3 4
1 3
2 4
stdout
Standard output is empty