fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxN=1e5+1;
  4. int p[maxN];
  5. int findP(int x){
  6. return (x==p[x])?x:p[x]=findP(p[x]);
  7. }
  8. int main(){
  9. ios::sync_with_stdio(0);
  10. cin.tie(0); cout.tie(0);
  11. int N, K, u, v;
  12. cin>>N>>K;
  13. int cnt=K;
  14. iota(p+1,p+N+1,1);
  15. for(int i=0; i<K; i++){
  16. cin>>u>>v;
  17. u=findP(u);
  18. v=findP(v);
  19. if(u!=v){
  20. cnt--;
  21. p[u]=v;
  22. }
  23. }cout<<cnt;
  24. }
Success #stdin #stdout 0.01s 5304KB
stdin
5 4
1 2
4 3
1 4
3 4
stdout
1