#include <bits/stdc++.h>
using namespace std;
struct Graph {
int n, m;
vector<int> head, next;
vector<int> src, dst;
Graph(int n) : n(n), m(0), head(n,-1) { }
int addEdge(int u, int v) {
next.push_back(head[u]);
src.push_back(u);
dst.push_back(v);
return head[u] = m++;
}
};
void dfs(Graph g) {
vector<int> visited(g.n);
function<void(int)> rec = [&](int u) {
cout << u << endl;
visited[u] = true;
int e = g.head[u];
while (e >= 0) {
if (!visited[g.dst[e]]) rec(g.dst[e]);
e = g.next[e];
}
};
rec(0);
}
int main() {
Graph g(3);
g.addEdge(0,1);
g.addEdge(1,2);
g.addEdge(2,0);
dfs(g);
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgR3JhcGggewogIGludCBuLCBtOwogIHZlY3RvcjxpbnQ+IGhlYWQsIG5leHQ7CiAgdmVjdG9yPGludD4gc3JjLCBkc3Q7CiAgR3JhcGgoaW50IG4pIDogbihuKSwgbSgwKSwgaGVhZChuLC0xKSB7IH0KICBpbnQgYWRkRWRnZShpbnQgdSwgaW50IHYpIHsKICAgIG5leHQucHVzaF9iYWNrKGhlYWRbdV0pOwogICAgc3JjLnB1c2hfYmFjayh1KTsKICAgIGRzdC5wdXNoX2JhY2sodik7CiAgICByZXR1cm4gaGVhZFt1XSA9IG0rKzsKICB9Cn07CnZvaWQgZGZzKEdyYXBoIGcpIHsKICB2ZWN0b3I8aW50PiB2aXNpdGVkKGcubik7CiAgZnVuY3Rpb248dm9pZChpbnQpPiByZWMgPSBbJl0oaW50IHUpIHsKICAgIGNvdXQgPDwgdSA8PCBlbmRsOwogICAgdmlzaXRlZFt1XSA9IHRydWU7CiAgICBpbnQgZSA9IGcuaGVhZFt1XTsKICAgIHdoaWxlIChlID49IDApIHsKICAgICAgaWYgKCF2aXNpdGVkW2cuZHN0W2VdXSkgcmVjKGcuZHN0W2VdKTsKICAgICAgZSA9IGcubmV4dFtlXTsKICAgIH0KICB9OwogIHJlYygwKTsKfQoKaW50IG1haW4oKSB7CiAgR3JhcGggZygzKTsKICBnLmFkZEVkZ2UoMCwxKTsKICBnLmFkZEVkZ2UoMSwyKTsKICBnLmFkZEVkZ2UoMiwwKTsKICBkZnMoZyk7Cn0K