fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int> ke[1001];
  4. int m , n;
  5. int parent[1001];
  6. bool visited[1001];
  7. int kt = 0;
  8. void DFS(int u) {
  9. visited[u] = true;
  10. for (int x : ke[u]) {
  11. if (!visited[x]) {
  12. cout << x << " ";
  13. parent[x] = u;
  14. DFS(x);
  15. kt = x; // C?p nh?t bi?n kt m?i khi tìm th?y m?t d?nh chua du?c tham
  16. }
  17. }
  18. }
  19.  
  20.  
  21. int main(){
  22. cin >> m >> n ;
  23. int s;
  24. cin >> s;
  25. for(int i = 1 ; i <= n ; i++){
  26. int x,y;
  27. cin >> x >> y;
  28. ke[x].push_back(y);
  29. }
  30. sort(ke[s].begin() , ke[s].end());
  31. cout << s << " ";
  32. DFS(s);
  33. // for(int x : ke[s]){
  34. // cout << x << " ";
  35. // }
  36. // vector<int> path ;
  37. // while(kt != s){
  38. // path.push_back(kt);
  39. // kt = parent[kt];
  40. // }
  41. // path.push_back(s);
  42. // reverse(path.begin() , path.end());
  43. // for(int x : path){
  44. // cout << x << " ";
  45. // }
  46. }
Success #stdin #stdout 0.01s 5280KB
stdin
5 10 4
5 1
4 5
4 2
4 3
1 5
3 5
5 3
3 1
1 4
5 2
stdout
4 2 3 5 1