Advertisement
Saleh127

at acl C

Sep 26th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4.  
  5. bool v[200005];
  6.  
  7. vector<ll>xx[200005];
  8.  
  9. void check(ll ind)
  10. {
  11. if(v[ind]==1) return;
  12. v[ind]=1;
  13.  
  14. for(ll i=0;i<xx[ind].size();i++)
  15. {
  16. ll nx=xx[ind][i];
  17. if(v[nx]==0)
  18. {
  19. check(nx);
  20. }
  21. }
  22. }
  23.  
  24. int main()
  25. {
  26. ll a,c,d,e,n,m,i,j,k=0;
  27. cin>>n>>m;
  28. for(i=0;i<m;i++)
  29. {
  30. cin>>a>>c;
  31. xx[a].push_back(c);
  32. xx[c].push_back(a);
  33. }
  34.  
  35. for(j=1;j<=n;j++)
  36. {
  37. if(v[j]==0)
  38. {
  39. k++;
  40. check(j);
  41. }
  42. }
  43. cout<<k-1<<endl;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement