#include using namespace std; #define ll long long #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define Pr pair #define Tp tuple using Graph = vector>; #define double long double const ll mod = 1000000007; //Unionfind struct unionfind { //自身が親であれば、その集合に属する頂点数に-1を掛けたもの //そうでなければ親のid vector r; vector act; unionfind(long long N) { r = vector(N, -1); act = vector(N,false); } long long root(long long x) { if (r[x] < 0) return x; return r[x] = root(r[x]); } bool unite(long long x, long long y) { x = root(x); y = root(y); if (x == y) return false; if (r[x] > r[y]) swap(x, y); r[x] += r[y]; r[y] = x; return true; } long long size(long long x) { return max(-r[root(x)],0LL); } // 2つのデータx, yが属する木が同じならtrueを返す bool same(long long x, long long y) { long long rx = root(x); long long ry = root(y); if(rx==ry){ act[rx] = true; } return rx == ry; } bool activate(ll x){ long long rx = root(x); return act[rx]; } }; //main関数内で unionfind friends(N+1); //などと宣言し friends.unite(a,b); //のように使う int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); ll N; cin >> N; ll a[N],b[N]; ll cnt[N+1]; rep(i,N) cnt[i+1] = 0; Graph G(N+1); rep(i,N){ cin >> a[i] >> b[i]; G[a[i]].push_back(i); G[b[i]].push_back(i); cnt[a[i]]++; cnt[b[i]]++; } unionfind balls(N+1); rep(i,N){ if(!balls.same(a[i],b[i])){ balls.unite(a[i],b[i]); } } rep(i,N){ if(!balls.activate(i+1)){ cout << "No" << endl; return 0; } } cout << "Yes" << endl; set used,app; ll ans[N]; rep(i,N) ans[i] = -1; queue go; rep(i,N){ app.insert(i+1); } ll cn = 0; rep(i,N){ if(cnt[i+1]==1){ go.push(i+1); } } rep(i,N){ if(a[i]==b[i]){ ans[i] = a[i]; app.erase(a[i]); used.insert(i); cn++; } } while(1){ if(cn!=N&&go.empty()){ auto itr = app.begin(); ll t = *itr; app.erase(itr); ll y = -1; for(ll x:G[t]){ if(!used.count(x)){ y = x; break; } } //cout << t << " " << y << endl; ans[y] = t; cnt[a[y]]--; cnt[b[y]]--; cnt[t] = -1; used.insert(y); if(cnt[a[y]]==1&&a[y]!=t){ go.push(a[y]); } if(cnt[b[y]]==1&&b[y]!=t){ go.push(b[y]); } cn++; } ll s = go.front(); go.pop(); for(ll x:G[s]){ if(used.count(x)) continue; ans[x] = s; app.erase(s); cnt[a[x]]--; cnt[b[x]]--; cnt[s] = -1; if(cnt[a[x]]==1&&app.count(a[x])){ go.push(a[x]); } if(cnt[b[x]]==1&&app.count(b[x])){ go.push(b[x]); } used.insert(x); cn++; } if(cn==N) break; } rep(i,N){ cout << ans[i] << endl; } }