#!/usr/bin/env python3 import sys from collections import defaultdict from collections import deque class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n """ 要素xの値を取得。""" def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] """ 2つの要素の併合。""" def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x return """ 要素xの属する集合の要素数を取得。""" def size(self, x): return -self.parents[self.find(x)] """ 2つの要素が同一の集合に属するか。""" def same(self, x, y): return self.find(x) == self.find(y) """ 要素xと同一の集合の要素を全取得。 計算量 : O(N) """ def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] """ 各集合の根を全取得。 計算量 : O(N) """ def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] """ 集合の個数を取得。 計算量 : O(N) """ def group_count(self): return len(self.roots()) """ 全集合の要素一覧を取得。 計算量 : O(N) """ def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) def main(): N = int(input()) uf = UnionFind(N) G = [[] for _ in range(N)] for nn in range(N): a, b = map(lambda i: int(i) - 1, input().split()) G[a].append((b, nn)) # 答えは順番に出力するので保持しておく必要がある。 if a != b: G[b].append((a, nn)) uf.union(a, b) groups = uf.all_group_members() for _, linked in groups.items(): link_num = 0 for i in linked: link_num = len(G[i]) if link_num != len(linked): print("No") return uu = UnionFind(N) ans = [-1] * N L=[0]*(N+1) T=[0]*N # print(G) # print(groups) # 各部分連結Cの根をみる for root in uf.roots(): flag = False # その根に属する全ての頂点linkedについて、 for linked in groups[root]: # 頂点linkedから移動可能なnodeと入力のindexととり、 for node, index in G[linked]: # print(node, index, T) # if T[index]==0: # T[index]=1 # 移動元lonkedと移動先nodeが既に連結しているならそれはサイクルなのでフラグを立てる。 if uu.same(linked,node): alpha=linked flag=True K = index break else: uu.union(linked, node) # print(flag) if flag: break ans[K]=alpha Q=deque([alpha]); L[alpha]=1 while Q: x=Q.popleft() for y,k in G[x]: if L[y]==0: if k==K: continue L[y]=1 Q.append(y) ans[k]=y print("Yes") for i in ans: print(i + 1) # print(ans) return if True: is_cycle=True root=-1 cycle_cnt=0 print(linked) for point in linked: for link_node, num in G[point]: if link_node == point: # 始点と終点が同じなので自己ループ cycle_cnt += 1 is_cycle = False ans[num] = point + 1 root = point if cycle_cnt > 1: print("No") return if is_cycle: next_point=-1 que=deque() que.append([linked[0],-1]) print(is_cycle) print(ans) print(root) sys.setrecursionlimit(10 ** 9) # seen = [False] * N firstOrder = [] lastOrder = [] def dfs(nodes: "list[list[int]]", now: int): firstOrder.append(now) seen[now] = 1 for next, _ in nodes[now]: if seen[next]: continue dfs(nodes, next) lastOrder.append(now) # while que: # q=que.pop() # for j in G[q[0]]: # if j[1]==q[1]: # continue # if seen[j[0]]: # root=j[0] # next_point=q[0] # ans[j[1]]=root+1 # que.clear() # break # que.append([j[0],j[1]]) # seen[q[0]]=1 dfs(G, linked[0]) print(firstOrder) print(lastOrder) print(seen) if next_point==-1: print("No") return dq=deque() dq.append(root) while dq: q=dq.pop() for j in G[q]: if ans[j[1]]==-1: ans[j[1]]=j[0]+1 dq.append(j[0]) for i in ans: if i==-1: print("No") return print("Yes") for i in ans: print(i) return # E = 0 # for i in nodes: # E = len(G[i]) # if E != len(nodes): # print("No") # return # ans = 0 # print(ans) return if __name__ == '__main__': main()