Advertisement
Guest User

Untitled

a guest
Nov 9th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include<vector>
  3. using namespace std;
  4.  
  5. int n;
  6. vector<int>adj[1000];
  7. bool visited[1000];
  8. bool initialize()
  9. {
  10. for(int i=0;i<=n;i++)
  11. visited[i]=false;
  12. }
  13. void bfs(int x)
  14. {
  15. initialize();
  16. int cnt=0;
  17. queue<int>q;
  18. q.push(x);
  19. while(!q.empty())
  20. {
  21. int u;
  22. u=q.front();
  23. q.pop();
  24. for(int i=0;i<adj[u].size();i++)
  25. {
  26. int v=adj[u][i];
  27. // cout<<"vis["<<v<<"]= "<<visited[v]<<endl;
  28. if(!visited[v])
  29. {
  30. cnt++;
  31. visited[v]=true;
  32. q.push(v);
  33. }
  34. }
  35. }
  36. cout<<n-cnt;
  37. for(int i=1;i<=n;i++)
  38. {
  39.  
  40. if(!visited[i])
  41. cout<<" "<<i;
  42. }
  43. cout<<endl;
  44. }
  45. int main()
  46. {
  47.  
  48. ios_base::sync_with_stdio(false);
  49. cin.tie(0);
  50. cout.tie(0);
  51. while(cin>>n && n)
  52. {
  53. int u,v;
  54.  
  55. while(cin>>u && u)
  56. {
  57. adj[u].clear();
  58. while(cin>>v && v)
  59. {
  60. adj[u].push_back(v);
  61. }
  62. }
  63.  
  64. int m;
  65. cin>>m;
  66. while(m--)
  67. {
  68. int x;
  69. cin>>x;
  70. bfs(x);
  71. }
  72. }
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement