fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector<vector<int> > adj(100);
  6.  
  7. map<int , bool> mp;
  8.  
  9. void print(int node , vector<int> &path){
  10. path.push_back(node);
  11. if(adj[node].size() == 0 && mp[node] == false){
  12. mp[node] = true;
  13. for(int i : path) cout << i << " ";
  14. cout << endl;
  15. }else{
  16. for(int i : adj[node]){
  17. print(i , path);
  18. }
  19. }
  20. path.pop_back();
  21. }
  22.  
  23.  
  24. void solve(){
  25. vector<int> path;
  26. print(1 , path);
  27. }
  28.  
  29.  
  30. int main(){
  31. int n; cin >> n;
  32. for(int i = 0; i < n; i++){
  33. int a,b; cin >> a >> b;
  34. adj[a].push_back(b);
  35. }
  36. solve();
  37. return 0;
  38. }
Success #stdin #stdout 0s 5292KB
stdin
5
1 2
1 3
2 4
3 5
stdout
1 2 4 
1 3 5