/** * @FileName a.cpp * @Author kanpurin * @Created 2021.08.07 05:36:45 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; struct StronglyConnectedComponents { private: vector> g, rg; vector order; vector visited; int V; void dfs(int v) { if (visited[v]) return; visited[v] = true; for (int u : g[v]) dfs(u); order.push_back(v); } void rdfs(int v, int c) { if (comp[v] != -1) return; comp[v] = c; for (int u : rg[v]) rdfs(u, c); } public: vector> Graph; vector comp; StronglyConnectedComponents(int v) : V(v) { g.resize(v); rg.resize(v); visited.resize(v); comp.resize(v); } void add_vertex(int n = 1) { V += n; g.resize(V); rg.resize(V); visited.resize(V); comp.resize(V); } void add_edge(int from, int to) { g[from].push_back(to); rg[to].push_back(from); } void build() { Graph = vector>(); order = vector(); visited.assign(V, false); comp.assign(V, -1); for (int i = 0; i < V; i++) dfs(i); reverse(order.begin(), order.end()); int number = 0; for (int i = 0; i < V; i++) { if (comp[order[i]] == -1) { rdfs(order[i], number++); } } Graph.resize(number); for (int i = 0; i < V; i++) { for (int v : g[i]) { if (comp[i] == comp[v]) continue; Graph[comp[i]].push_back(comp[v]); } } } }; struct TwoSAT { private: int _n; std::vector _answer; StronglyConnectedComponents scc; public: TwoSAT():_n(0),scc(0){} explicit TwoSAT(int n) : _n(n), _answer(n), scc(2*n) {} void add_clause(int i, bool f, int j, bool g) { assert(0 <= i && i < _n); assert(0 <= j && j < _n); scc.add_edge(2 * i + (f ? 0 : 1), 2 * j + (g ? 1 : 0)); scc.add_edge(2 * j + (g ? 0 : 1), 2 * i + (f ? 1 : 0)); } void only_one(const vector &x, const vector &t) { assert(x.size() == t.size()); int _m = x.size(); if (_m <= 12) { for (int i = 0; i < _m-1; i++) { for (int j = i+1; j < _m; j++) { add_clause(x[i],!t[i],x[j],!t[j]); } } } int _nn = _n; _n += _m*2; scc.add_vertex(_m*4); _answer.resize(_n); for (int i = 0; i < _m; i++) { add_clause(_nn+i,false,x[i],!t[i]); add_clause(_nn+_m+i,false,x[i],!t[i]); } for (int i = 1; i < _m; i++) { add_clause(_nn+i,false,_nn+i-1,true); add_clause(x[i],!t[i],_nn+i-1,true); add_clause(_nn+_m+i-1,false,_nn+_m+i,true); add_clause(x[i-1],!t[i-1],_nn+_m+i,true); } } bool satisfiable() { scc.build(); auto id = scc.comp; for (int i = 0; i < _n; i++) { if (id[2 * i] == id[2 * i + 1]) return false; _answer[i] = id[2 * i] < id[2 * i + 1]; } return true; } std::vector answer() { return _answer; } }; int main() { int n;cin >> n; TwoSAT ts(n); vector a(n),b(n); vector> x(n); vector> t(n); for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; x[a[i]-1].push_back(i); x[b[i]-1].push_back(i); t[a[i]-1].push_back(true); t[b[i]-1].push_back(false); } for (int i = 0; i < n; i++) { ts.only_one(x[i],t[i]); } if (!ts.satisfiable()) { puts("No"); } else { puts("Yes"); auto ans = ts.answer(); for (int i = 0; i < n; i++) { cout << (ans[i]?a[i]:b[i]) << endl; } } return 0; }