Advertisement
hkshakib

Untitled

Jun 29th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. map<int, int> visited;
  5.  
  6. int BFS(int x, map<int, vector<int> > G)
  7. {
  8. queue<int> q;
  9. q.push(x);
  10. visited[x]=0;
  11. while(!q.empty())
  12. {
  13. int u=q.front();
  14. q.pop();
  15. for(int i=0; i<G[u].size(); i++)
  16. {
  17. int d=G[u][i];
  18. if(!visited.count(d))
  19. {
  20. visited[d]=visited[u]+1;
  21. q.push(d);
  22. }
  23. }
  24.  
  25. }
  26.  
  27. }
  28.  
  29. int main()
  30. {
  31. int nc, k=0;
  32. while(cin>>nc)
  33. {
  34. if(nc==0)
  35. break;
  36. map<int, vector<int> > node;
  37.  
  38. int x, y;
  39. for(int i=0; i<nc; i++)
  40. {
  41. cin>>x>>y;
  42. node[x].push_back(y);
  43. node[y].push_back(x);
  44. }
  45.  
  46. int s, ttl;
  47. while(scanf("%d%d", &s, &ttl)==2)
  48. {
  49. if(s==0 && ttl==0)
  50. break;
  51.  
  52. map<int, int>::iterator it;
  53. visited.clear();
  54. BFS(s, node);
  55. int cnt=0;
  56.  
  57. for(it=visited.begin(); it!=visited.end(); ++it)
  58. {
  59. if((*it).second>ttl)
  60. ++cnt;
  61. }
  62.  
  63. int ans=cnt+node.size()-visited.size();
  64.  
  65. printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n", ++k, ans, s, ttl);
  66. }
  67. }
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement