#include #include using namespace std; using namespace atcoder; using graph = vector>>; vector a, b; int cycle(const graph &to, vector &seen, int v, int e) { seen.at(v) = true; for (auto &&[c, e_idx] : to.at(v)) { if (e_idx == e) { continue; } if (seen.at(c)) { return e_idx; } int res = cycle(to, seen, c, e_idx); if (res != -1) { return res; } } return -1; } void dfs(const graph &to, vector &ans, vector &rev, int v) { for (auto &&[c, e_idx] : to.at(v)) { if (ans.at(e_idx) != -1) { continue; } if (rev.at(a.at(e_idx))) { ans.at(e_idx) = b.at(e_idx); } else { ans.at(e_idx) = a.at(e_idx); } rev.at(ans.at(e_idx)) = true; dfs(to, ans, rev, c); } } int main() { int n; cin >> n; a = vector(n); b = vector(n); for (int i = 0; i < n; i++) { cin >> a.at(i) >> b.at(i); a.at(i)--; b.at(i)--; } dsu t(n); for (int i = 0; i < n; i++) { t.merge(a.at(i), b.at(i)); } auto gs = t.groups(); int sz = gs.size(); vector idx(n), idx_in(n); vector to(sz); for (int i = 0; i < sz; i++) { int sz_in = gs.at(i).size(); to.at(i) = graph(sz_in); for (int j = 0; j < sz_in; j++) { idx.at(gs.at(i).at(j)) = i; idx_in.at(gs.at(i).at(j)) = j; } } for (int i = 0; i < n; i++) { int k = idx.at(a.at(i)), x = idx_in.at(a.at(i)), y = idx_in.at(b.at(i)); assert(k == idx.at(b.at(i))); to.at(k).at(x).emplace_back(y, i); to.at(k).at(y).emplace_back(x, i); } for (int i = 0; i < sz; i++) { graph to_in = to.at(i); int k = to_in.size(), s = 0; for (int j = 0; j < k; j++) { s += to_in.at(j).size(); } if (s != 2 * k) { puts("No"); return 0; } } puts("Yes"); vector ans(n, -1); vector rev(n); for (int i = 0; i < sz; i++) { auto gs_in = gs.at(i); int sz_in = gs_in.size(); graph to_in = to.at(i); vector seen(sz_in); int e_idx = cycle(to_in, seen, 0, -1); ans.at(e_idx) = a.at(e_idx); rev.at(a.at(e_idx)) = true; dfs(to_in, ans, rev, idx_in.at(a.at(e_idx))); } for (int i = 0; i < n; i++) { cout << ans.at(i) + 1 << endl; } return 0; }