Advertisement
Saleh127

Hacker earth Level nodes

Oct 26th, 2020
72
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  6. vector<ll>g[10000];
  7. bool v[10000];
  8. ll dis[10000];
  9. ll ans=0,root;
  10. void bfs(ll r)
  11. {
  12. queue<ll>qq;
  13. v[r]=1;
  14. dis[r]=1;
  15. qq.push(r);
  16. while(!qq.empty())
  17. {
  18.  
  19. ll node=qq.front();
  20.  
  21. qq.pop();
  22.  
  23. for(ll i=0; i<g[node].size(); i++)
  24. {
  25. ll next=g[node][i];
  26.  
  27. if(v[next]==0)
  28. {
  29. v[next]=1;
  30. dis[next]=dis[node]+1;
  31. if(dis[next]==root)
  32. {
  33. ans++;
  34. }
  35. qq.push(next);
  36. }
  37. }
  38. }
  39. }
  40.  
  41.  
  42. int main()
  43. {
  44. ios_base::sync_with_stdio(0);
  45. cin.tie(0);
  46. cout.tie(0);
  47.  
  48. ll n,m,a,b,i,j,k,l;
  49.  
  50. cin>>m;
  51. for(i=0; i<=m; i++)
  52. {
  53. g[i].clear();
  54. v[i]=0;
  55. }
  56. for(i=1; i<m; i++)
  57. {
  58. cin>>a>>b;
  59. g[a].push_back(b);
  60. g[b].push_back(a);
  61. }
  62. cin>>root;
  63. ans=0;
  64. bfs(1);
  65.  
  66. cout<<ans<<endl;
  67. return 0;
  68. }
  69.  
Advertisement
RAW Paste Data Copied
Advertisement