Guest User

Untitled

a guest
Jul 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <stack>
  4. #include <vector>
  5. #include <algorithm>
  6. using namespace std;
  7. vector<int> v[1000];
  8. vector<bool> check(1000);
  9. queue<int> qu;
  10. vector<int> find;
  11. int n,m,start;
  12. void dfs(int present)
  13. {
  14. if(check[present]==false)
  15. {
  16. check[present]=true;
  17. cout<<present<<" ";
  18. for(int i=0; i<v[present].size(); i++)
  19. {
  20. if(check[v[present][i]]==false)
  21. dfs(v[present][i]);
  22. }
  23. }
  24. else
  25. {
  26. return;
  27. }
  28. }
  29. int main() {
  30. cin>>n>>m>>start;
  31. for(int i=0; i<m; i++)
  32. {
  33. int a,b;
  34. cin>>a>>b;
  35. v[a].push_back(b);
  36. v[b].push_back(a);
  37. }
  38. for(int i=0; i<n; i++)
  39. {
  40. sort(v[i].begin(), v[i].end());
  41. }
  42. for(int i=1; i<=n; i++)
  43. {
  44. check[i]=false;
  45. }
  46. dfs(start);
  47.  
  48.  
  49.  
  50. cout<<endl;
  51.  
  52.  
  53. //bfs
  54. for(int i=1; i<=n; i++)
  55. {
  56. check[i]=false;
  57. }
  58. qu.push(start);
  59. cout<<start<<" ";
  60. check[qu.front()]=true;
  61. while(!qu.empty())
  62. {
  63. for(int i=0; i<v[qu.front()].size(); i++)
  64. {
  65. if(check[v[qu.front()][i]]==false)
  66. {
  67. qu.push(v[qu.front()][i]);
  68. cout<<v[qu.front()][i]<<" ";
  69. check[v[qu.front()][i]]=true;
  70. }
  71. }
  72. qu.pop();
  73. }
  74. cout<<endl;
  75. }
  76. //탐색알고리즘
  77. //그래프[DFS,BFS] - 노드, 엣지(간선)
  78. //vector<int> v[5]; - 가진 노드수 만큼 만들어놓기
  79. //인접 리스트와 인접행렬의 개념
Add Comment
Please, Sign In to add comment