#include <iostream>
#include <vector>
#include <stack>
#include <utility>
using namespace std;

void rundfs(const vector<vector<int> >& graph, const int max) {
vector<bool> visited(max);
stack<pair<bool,int>> dfs;
stack<int> postOrder;
for(int i=0 ; i < max ; i++){
    if(!visited[i]){
        dfs.push(make_pair(false,i));
    }   
    while(!dfs.empty()){
        pair<bool,int> node=dfs.top();
        dfs.pop();
        if (node.first) {
        	cout << node.second << endl;
            postOrder.push(node.second);
            continue;
        }
        if (visited[node.second]) {
        	continue;
        }
        visited[node.second]=true;
        dfs.push(make_pair(true, node.second));
        const auto& newVec=graph[node.second]; //vector of neighboors
        for(const auto son: newVec){
            if(!visited[son]){
                dfs.push(make_pair(false, son));
            }
        }
    }
}

}

int main() {
	rundfs({ {1, 2, 3},   {3},   {1, 3},   {}}, 4);
	return 0;
}
