fork download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. void dfs(int start, vector<int> graph[], bool check[]){
  8.  
  9. stack<int> s;
  10. s.push(start);
  11. check[start] = true;
  12. printf("%d ",start);
  13.  
  14. while(!s.empty()){
  15.  
  16. int current_node = s.top();
  17. s.pop();
  18. for(int i=0; i<graph[current_node].size(); i++){
  19.  
  20. int next_node = graph[current_node][i];
  21.  
  22. if(check[next_node]==false){
  23. printf("%d ", next_node);
  24. check[next_node] = true;
  25. // pop()을 했었기 때문에 현재 current_node도 넣어줘야한다.
  26. s.push(current_node);
  27. s.push(next_node);
  28. break;
  29. }
  30. }
  31. }
  32.  
  33. }
  34.  
  35. int main (){
  36.  
  37.  
  38. int n, m, start;
  39. cin >> n >> m >> start;
  40.  
  41. vector<int> graph[n+1];
  42. bool check [n+1];
  43. fill(check, check+n+1, false);
  44.  
  45. for(int i=0; i<m; i++){
  46. int u,v;
  47. cin >> u >> v;
  48.  
  49. graph[u].push_back(v);
  50. graph[v].push_back(u);
  51. }
  52.  
  53. // Sorting은 왜 한거지?
  54. // 나중에 하나의 정점에서 다음을 탐색할 때 node 값에 순차적으로 접근해야하기 때문
  55. for(int i=1; i<=n; i++){
  56. sort(graph[i].begin(), graph[i].end());
  57. }
  58.  
  59. //dfs
  60. dfs(start, graph, check);
  61. printf("\n");
  62.  
  63. return 0;
  64. }
  65.  
  66.  
  67. // dfs에 들어오면 '방문'한거로 판단
  68. // 해당 위치에 check true로 해준다.
  69. void dfs(int start, vector<int> graph[], bool check[]){
  70. check[start]= true;
  71. printf("%d ", start);
  72.  
  73. for(int i=0; i < graph[start].size(); i++){
  74. int next = graph[start][i];
  75. // 방문하지 않았다면
  76. if(check[next]==false){
  77. // 재귀함수를 호출한다.
  78. dfs(next, graph, check);
  79. }
  80. }
  81. }
  82.  
  83. int main (){
  84.  
  85. int n, m, start;
  86. cin >> n >> m >> start;
  87.  
  88. vector<int> graph[n+1];
  89. bool check [n+1];
  90. fill(check, check+n+1, false);
  91.  
  92. for(int i=0; i<m; i++){
  93. int u,v;
  94. cin >> u >> v;
  95.  
  96. graph[u].push_back(v);
  97. graph[v].push_back(u);
  98. }
  99.  
  100. // Sorting은 왜 한거지?
  101. // 나중에 하나의 정점에서 다음을 탐색할 때 node 값에 순차적으로 접근해야하기 때문
  102. for(int i=1; i<=n; i++){
  103. sort(graph[i].begin(), graph[i].end());
  104. }
  105.  
  106. //dfs
  107. dfs(start, graph, check);
  108. printf("\n");
  109.  
  110. return 0;
  111. }
  112.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘void dfs(int, std::vector<int>*, bool*)’:
prog.cpp:9:2: error: ‘stack’ was not declared in this scope
  stack<int> s;
  ^~~~~
prog.cpp:9:2: note: ‘std::stack’ is defined in header ‘<stack>’; did you forget to ‘#include <stack>’?
prog.cpp:5:1:
+#include <stack>
 using namespace std;
prog.cpp:9:2:
  stack<int> s;
  ^~~~~
prog.cpp:9:8: error: expected primary-expression before ‘int’
  stack<int> s;
        ^~~
prog.cpp:10:2: error: ‘s’ was not declared in this scope
  s.push(start);
  ^
prog.cpp: At global scope:
prog.cpp:69:6: error: redefinition of ‘void dfs(int, std::vector<int>*, bool*)’
 void dfs(int start, vector<int> graph[], bool check[]){
      ^~~
prog.cpp:7:6: note: ‘void dfs(int, std::vector<int>*, bool*)’ previously defined here
 void dfs(int start, vector<int> graph[], bool check[]){
      ^~~
prog.cpp:83:5: error: redefinition of ‘int main()’
 int main (){
     ^~~~
prog.cpp:35:5: note: ‘int main()’ previously defined here
 int main (){
     ^~~~
stdout
Standard output is empty