#include #include #define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) using namespace atcoder; using namespace std; typedef long long ll; void solve(int n, int m, vector u, vector v) { map ma; vector> gr(n); rep(i, 0, m) { gr[u[i]].push_back(v[i]); ma[v[i]]++; } priority_queue, greater> pq; rep(i, 0, n) { if (ma[i] == 0) pq.push(i); } vector ans; while (!pq.empty()) { int x; x = pq.top(); pq.pop(); ans.push_back(x); for (auto y : gr[x]) { ma[y]--; if (ma[y] == 0) { pq.push(y); } } } rep(i, 0, n) cout << ans[i] + 1 << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int ind = -1; vector u, v; rep(i, 0, n) { int a, b; cin >> a >> b; if (a == 1) { u.emplace_back(i + 1); v.emplace_back(i); } if (b == 2) ind = i; if (b == 1) { u.emplace_back(i); v.emplace_back(ind); } } int m = u.size(); solve(n, m, u, v); }