Advertisement
Saleh127

hackerearcth Unreachable Nodes

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